1

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.

1
  • That is not a very good way of debugging. If you would have used something like 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 $count being undefined. You should familiarize yourself with these tools for development. Another side note is that you should not be using the deprecated mysql_* functions. I would suggest familiarizing yourself with mysqli or PDO. Commented Jan 22, 2015 at 17:28

1 Answer 1

2
$countt 

has two t's

if($count>0)

so $count is undefined

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

2 Comments

For a trivial typo like this, just post a comment and vote to close.
Yep. that did it. missed that somehow.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.