Link to home
Start Free TrialLog in
Avatar of ncomper
ncomper

asked on

Simple Count in PHP

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);
?>
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

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

HTH,
Dan
Avatar of ncomper
ncomper

ASKER

Sorry I dont understand ?
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

Avatar of ncomper

ASKER

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 ?
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.
Avatar of ncomper

ASKER

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
Avatar of ncomper

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial