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("local host","xxx x","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);
?>
<?php
$con=mysqli_connect("local
// 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);
?>
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:
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);
?>
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.
Post the output, please.
<?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);
?>
Post the output, please.
I took Dan's code from https://www.experts-exchange.com/questions/28383752/Simple-Count-in-PHP.html?anchorAnswerId=39915189#a39915189 , put my own info in it, and it worked perfectly.
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
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
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you want some background on how to use the MySQL extensions, this article may be helpful:
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
if your table is called em_subscribers, then the query will return 0 results.
HTH,
Dan