Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-18-2013, 06:27 AM   PM User | #1
epheterson
New Coder

 
Join Date: Nov 2007
Posts: 50
Thanks: 2
Thanked 0 Times in 0 Posts
epheterson is an unknown quantity at this point
Question 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 ( )
epheterson is offline   Reply With Quote
Old 01-18-2013, 06:33 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 01-18-2013, 07:14 AM   PM User | #3
epheterson
New Coder

 
Join Date: Nov 2007
Posts: 50
Thanks: 2
Thanked 0 Times in 0 Posts
epheterson is an unknown quantity at this point
Never in my life have I spent so much time on such a tiny thing. I hope somebody finds this page someday, thanks.
epheterson is offline   Reply With Quote
Old 01-18-2013, 02:35 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by epheterson View Post
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:24 AM.


Advertisement
Log in to turn off these ads.