?
Solved

Simple Count in PHP

Posted on 2014-03-08
10
Medium Priority
?
734 Views
Last Modified: 2014-03-09
Im trying to display a simple count query on a PSP page but it keeps coming up blank is there.  Should I have an error for the "show count" statement also


<?php
$con=mysqli_connect("localhost","xxxx","xxxx","xxxx");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$query = "SELECT COUNT(*) from em_subsribers";
$result = mysql_query($query);
$values = mysql_fetch_assoc($result);
$num_rows = $values['total'];
echo $num_rows;

mysqli_close($con);
?>
0
Comment
Question by:ncomper
10 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39915185
On this: $query = "SELECT COUNT(*) from em_subsribers";
if your table is called em_subscribers, then the query will return 0 results.

HTH,
Dan
0
 
LVL 5

Author Comment

by:ncomper
ID: 39915188
Sorry I dont understand ?
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39915189
BTW, you have multiple problems in that code. You're mixing and matching mysql and mysqli, and that's not good.
Plus, you have no field called total in your query, so why it should return one?
Try this:
<?php
$con=mysqli_connect("localhost","xxxx","xxxx","xxxx");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$query = "SELECT COUNT(*) AS total FROM em_subscribers"; 
$result = mysqli_query($con, $query); 
$values = mysqli_fetch_assoc($result); 
$num_rows = $values['total']; 
echo $num_rows;

mysqli_close($con);
?>

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 5

Author Comment

by:ncomper
ID: 39915203
No just a blank page :( but if I put in the wrong sql details it will show me an error so its logs into the DB ?
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39915216
Then it's time for debugging.
<?php
$con=mysqli_connect("localhost","xxxx","xxxx","xxxx");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$query = "SELECT COUNT(*) AS total FROM em_subscribers"; 
echo "<pre>";
print_r ($query);
$result = mysqli_query($con, $query); 
echo "<pre>";
print_r ($result);
$values = mysqli_fetch_assoc($result); 
echo "<pre>";
print_r ($values);
$num_rows = $values['total']; 
echo $num_rows;
echo "<pre>";
print_r ($num_rows);

mysqli_close($con);
?>

Open in new window


Post the output, please.
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39915273
I took Dan's code from http://www.experts-exchange.com/Database/MySQL/Q_28383752.html#a39915189 , put my own info in it, and it worked perfectly.
0
 
LVL 5

Author Comment

by:ncomper
ID: 39915638
Amazing that worked. However got this as a result.  

how would I hide the cost here so I just had

Total = XXXXX

Thanks ever so much

Terry


SELECT COUNT(*) AS total FROM em_subscriber
mysqli_result Object
(
    [current_field] => 0
    [field_count] => 1
    [lengths] =>
    [num_rows] => 1
    [type] => 0
)
Array
(
    [total] => 281504
)
281504
281504
0
 
LVL 5

Author Comment

by:ncomper
ID: 39915677
Fantastic that worked, however how do I hide all the code from the page (as this is what I got below).  Now all I really want is:

Total = XXXX


SELECT COUNT(*) AS total FROM em_subscriber
mysqli_result Object
(
    [current_field] => 0
    [field_count] => 1
    [lengths] =>
    [num_rows] => 1
    [type] => 0
)
Array
(
    [total] => 281504
)
281504
281504
0
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 2000 total points
ID: 39915696
Remove the debugging code:
<?php
$con=mysqli_connect("localhost","xxxx","xxxx","xxxx");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$query = "SELECT COUNT(*) AS total FROM em_subscribers"; 
$result = mysqli_query($con, $query); 
$values = mysqli_fetch_assoc($result); 
$num_rows = $values['total']; 
echo "Total = " . $num_rows;

mysqli_close($con);
?>

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39915800
If you want some background on how to use the MySQL extensions, this article may be helpful:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question