0

I'm trying to get the total number of rows in a table using this code:

$count = mysqli_query($con, "SELECT COUNT(*) AS total FROM random_codes");
$count = mysqli_fetch_object($count);
$count = $count->total;

echo "count is $count<br />";

but the echo is always "count is " with no value following. Runing the same SQL code in phpMyAdmin returns the number of rows (above 2000) as total like requested, so the problem is probably in the php code. Dear responders, I'm new to php & sql so please elaborate and/or link me to documentation. Thanks..

EDIT: attempted error checking, no error shows

$result = mysqli_query($con, "SELECT COUNT(*) AS total FROM random_codes");
echo mysqli_error($con);
$obj = mysqli_fetch_object($result);
$count = $obj->total;
echo "count is $count<br />";
5
  • don't write everything in one variable Commented Apr 27, 2013 at 15:34
  • 1
    You need to check for mysqli errors. And PHP errors in general Commented Apr 27, 2013 at 15:38
  • try not to use $count for all variable names. Commented Apr 27, 2013 at 16:03
  • @Your, I meant it when I said I'm new to php and sql. Opend your chain of links but I can't make much out of it because a lot of the syntax and design patterns are unknown to me. Is there any good tutorial for this error checking thing you can link me to? Commented Apr 27, 2013 at 17:22
  • I tried your codes, both works. Are you sure you can see "count is" string with no number, even zero? What exactly do you see? Commented Apr 27, 2013 at 17:46

2 Answers 2

2

I was able to duplicate your problem by turning off PHP's error reporting, and making a bad database connection:

error_reporting(0);

$con = new mysqli("localhost", "baduser", "badpw", "SomeDB");

$count = mysqli_query($con, "SELECT COUNT(*) AS total FROM SomeTable");
echo mysqli_error($con);
$count = mysqli_fetch_object($count);
$count = $count->total;

echo "count is $count<br />";

Output: count is

Try turning on error reporting and double checking your connection information:

error_reporting(E_ALL);

Perhaps that may give you an error such as:

Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'baduser'@'localhost' (using password: YES)

If you are on a shared host, often times they default error reporting to a value that may hide certain errors for you, and you'll need to override that while in development mode.

Sign up to request clarification or add additional context in comments.

3 Comments

Your answer is painfully right, I did get a kind of error message, but not printed - this is what chrome told me: 500 HTTP (Internal Server Error):
#vcardillo do you know of a good free online php instructor? one that teaches more than syntax?
Glad you figured it out! :) Not off the top of my head, unfortunately. What I have learned has mostly been from experience working on large PHP applications. I looked through Amazon quickly, and didn't find a book I'd likely recommend either. I know what you're asking, and it's for something like "Application Programming with PHP" or "Enterprise PHP Development." Here are some links I found in searching: us1.php.net/support.php talks.php.net amzn.com/1430219742 Maybe someone else here can suggest some resources, too.
1

I think the problem is in the echo statement.What u need to do is use a . operator between "count is" and the variable $count.

echo "count is". $count."<br />";

If this doesnot work try this.

$query=mysql_query("SELECT COUNT(*) AS total FROM random_codes");
$result=mysql_fetch_array($query);
$count=$result['total'];
echo "count is". $count."<br />";

2 Comments

I was wondering why someone bothered downvoting both answsers here, but reading your answer I guess I know why (even though I would give you a break, I know you meant for good). To help you avoid downvots, read this: trans4mind.com/personal_development/phpTutorial/quotes.htm
@YekhezkelYovel that's simple. these answers can't be of any help. That's what downvote exactly for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.