CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Should be simple... echo count of rows in MySQL table (http://www.codingforums.com/showthread.php?t=285990)

epheterson 01-18-2013 06:27 AM

Should be simple... echo count of rows in MySQL table
 
PHP Code:

<?php

$server
="localhost";
$user="user";
$password="password";
$database="database";

$db = new mysqli($server,$user,$password,$database);
if (
$db->connect_errno) {
printf("Connect failed: %s\n"$mysqli->connect_error);
exit();
}

$q 'SELECT COUNT(*) FROM example';
$r=mysqli_query($db,$q) or die(mysqli_error($db)." Q=".$q);
$r mysql_fetch_row($r);
echo 
$r[0];

?>

For the life of me, I cannot see why this returns 0, or print_r($db) -> empty SQL array
Code:

mysqli_result Object ( )

Fou-Lu 01-18-2013 06:33 AM

Your issue is that you are combining MySQLi and MySQL libraries together. If you enable your error reporting it should complain that mysql_fetch_array expects a resource, but the mysqli_query returns a mysqli_result object.
Simply add the 'i' into the mysqli_fetch_row and it will work fine.

epheterson 01-18-2013 07:14 AM

Never in my life have I spent so much time on such a tiny thing. I hope somebody finds this page someday, thanks.

Fou-Lu 01-18-2013 02:35 PM

Quote:

Originally Posted by epheterson (Post 1307070)
Never in my life have I spent so much time on such a tiny thing. I hope somebody finds this page someday, thanks.

Lol, its amazing what one letter difference can do ;)
Since you are already using the $db in an object oriented fashion with the error checking, why not continue doing so in the rest of the execution? That would get around some of the problems like above. The incorrect parameter type should be classified by an E_WARNING level, while if you have the incorrect method completely it would be E_ERROR. So at least you'd get a white output if you don't have error reporting enabled versus normal output missing the desired data.
OO mysqli also removes the mysqli_ from the prefix of the methods, so its more difficult to make an error on that one.


All times are GMT +1. The time now is 01:44 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.