I have mySQL datanase installed, with phpMyAdmin to administer it. I have a test database with one "users" table and one row I have put in.
In phpMyAdmin, when I do "SELECT * from users" I am receiving my one row correctly.
Now I've built a small PHP hello world page.It connects to the very same database, with a fully privileged user credential I've opened. It submits the same query, but for some strange reason, the mysql_num_rows is always zero
Here's the code I use. Please note I am seeing the following debug messages on my web page when I load it:
success so far
success so far 2
No Rows
Here's the code I'm using:
<?php
if (!$link = mysql_connect('localhost', 'testuser', 'testpw')) {
echo 'Could not connect to mysql';
exit;
}
if (!mysql_select_db('MYDB', $link)) {
echo 'Could not select database';
exit;
}
echo 'success so far<br>';
$sql = 'SELECT * FROM users';
$result = mysql_query($sql, $link) or die(mysql_error());
if (!$result) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
echo 'success so far 2<br>';
$countt = mysql_num_rows($result);
if($count>0)
{
echo "we have rows";
}
else
{
echo "No Rows";
}
while ($row = mysql_fetch_assoc($result)) {
echo $row['foo'];
}
mysql_free_result($result);
?>
I'd appreciate any pointers as to what I'm doing wrong. As you can tell - I'm a php newbie.
var_dump($count)you likely would have spotted your error quickly. Also, if you have PHP notices turned on in your dev environment and are either displaying errors on screen or looking at your logs, you would have seen a notice about$countbeing undefined. You should familiarize yourself with these tools for development. Another side note is that you should not be using the deprecatedmysql_*functions. I would suggest familiarizing yourself withmysqliorPDO.