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 />";
  • don't write everything in one variable – monkeyinsight Apr 27 '13 at 15:34
  • 1
    You need to [check for mysqli errors](http://stackoverflow.com/questions/15447133/mysqli-update-throwing-call-to-a-member-function-bind-param-error/15447204#15447204). And PHP errors in general – Your Common Sense Apr 27 '13 at 15:38
  • try not to use $count for all variable names. – Mohit Padalia Apr 27 '13 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? –  Apr 27 '13 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? – Your Common Sense Apr 27 '13 at 17:46

2 Answers2

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.

vcardillo
  • 1,646
  • 3
  • 24
  • 29
  • 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): –  Apr 28 '13 at 20:15
  • #vcardillo do you know of a good free online php instructor? one that teaches more than syntax? –  Apr 28 '13 at 20:16
  • 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: http://us1.php.net/support.php http://talks.php.net/ http://amzn.com/1430219742 Maybe someone else here can suggest some resources, too. – vcardillo Apr 29 '13 at 03:45
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 />";
Raz Mahato
  • 875
  • 1
  • 7
  • 11
  • 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: http://www.trans4mind.com/personal_development/phpTutorial/quotes.htm –  Apr 27 '13 at 17:07
  • @YekhezkelYovel that's simple. these answers can't be of any help. That's what downvote exactly for. – Your Common Sense Apr 27 '13 at 17:32