mysql_num_rows() not working

I read the help into at and actually used that code for what I was doing. I want to know how many entries are in a given table. said to use the mysql_num_rows() function to find this number. So I did. But…

Here is the code

<?php $db = mysql_connect("host", "user", "pass"); mysql_select_db("photoalbum", $db); $result = mysql_query("SELECT * from album_users", $db); 7 $num_rows = mysql_num_rows($result); for ($i = 0; $i < $num_rows; $i++) { $row = mysql_fetch_array($result); if (stripslashes($row["user_name"]) == $user_name && stripslashes($row["password"]) == $password) { . . . ?>

And the error I am getting

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/lskyeho/public_html/editalbum/script/login.php on line 7

This is the exact code. Thank you

Right after the line
$result = mysql_query(“SELECT * from album_users”, $db);


Add the following
echo "
MYSQL ERROR IS - “.mysql_error().”

Typically the error message you get is because MYSQL did not like a command. Adding the line I suggest should tell you more about what kind of error MySQL is having. (Wrong DB, Table doesn’t exist, etc…)

i have just checked the code, and please check this code, i think it may help u.

<?php $host="localhost"; $usr=""; $pass=""; $conn=mysql_connect($host,$usr,$pass) or die("Cannot connect"); $db=mysql_select_db("db",$conn) or die("Database not selected"); $qu="select * from std "; $result=mysql_query($qu,$conn); $num=mysql_num_rows($result); echo "
The rows are $num "; echo "n "; echo "
"; } echo "
ID NAME</th"; while($row=mysql_fetch_array($result)) { echo "
$row[std_id] $row[std_name]
n"; mysql_close($conn); ?>

This post was changed due to a unattentive quick response ~ Didn’t realize to seperate people were posting and misunderstood what hasnain was saying, along with thinking he was the Orginal poster. Sorry for the abrasive post everyone!

Thanks for pointing out my error Paul!

What Paul suggested helped because I was able to pin point the real issue.

Now I get three errors:

Warning: main(is_loginphp): failed to open stream: No such file or directory in /home/dir/public_html/editalbum/edit/index.php on line 10

Warning: main(): Failed opening ‘is_loginphp’ for inclusion (include_path=’.:/usr/lib/php:/usr/local/lib/php’) in /home/dir/public_html/editalbum/edit/index.php on line 10

Fatal error: Call to undefined function: is_login() in /home/dir/public_html/editalbum/edit/index.php on line 11

The problem is when I add mysql_error() again these errors are removed and a random on is added. So I add mysql_error() to that one (without removing anything) and that new error moves (not always down the file either (ie line 7, 20, 14…).

This new error will be something like:

Warning: Cannot modify header information - headers already sent by (output started at /home/ldir/public_html/editalbum/script/login.php:10) in /home/dir/public_html/editalbum/script/login.php on line 14

Thank you for the help

Here is more code to help

<?php $db = mysql_connect("host", "user", "pass") or die("Cannot connect to db"); mysql_select_db("user_photoalbum", $db) or die("Db not selected"); $result = mysql_query("SELECT * from album_users", $db); $num_rows = mysql_num_rows($result); for ($i = 0; $i < $num_rows; $i++) { $row = mysql_fetch_array($result); echo "
MYSQL ERROR".mysql_error()."
"; if (stripslashes($row["user_name"]) == $user_name && stripslashes($row["password"]) == $password) { echo "
MYSQL ERROR".mysql_error()."
"; $result = mysql_query("UPDATE album_users SET is_logged_in = 'Y' WHERE user_name = $user_name && password = $password"); Header('Location: ../edit/index.php?user_name=$user_name'); $i = $num_rows; } else { // go to index with error message user/pass no work $i = mysql_fetch_array($result); Header('Location: ../index.php?error=true'); } } ?>

Thanks again

The error about the “can not modify header information” is due to the fact that you have to have that at the very top of your page. Nothing can be outputed before that.

The next 3 errors are due to the fact that is_loginphp page doesn’t exsist or the path to it is wrong. I am thinking maybe you meant: is_login.php?

If that is the case then that should fix a couple of the errors. As for the “Call to undefined function: is_login()”, this error is because you have not defiend a function named is_login. Although I am not seeing where these are being used at and how, I would say these are the mostly likely cause of the errors.

I have written this code:


<?php include("../script/is_login.php?user_name=$user_name"); $inc = new is_login_class(); if ($inc->is_login($user_name) == false || $user_name == 'NULL') { ... ?>

Now in this include file:

<?php class is_login_class { function is_login($user_name) { ... } } ?>


MOD EDIT: Added PHP BB Code tags…
Added Note: Please read the post at the top of each forum on how to post messages (particularly the code and php tags)

This seems like it should work. I have checked my references and this seems like the general syntax

I am getting these errors

Warning: main(…/script/is_login.php?user_name=user): failed to open stream: No such file or directory in /home/dir/public_html/editalbum/edit/index.php on line 2

Warning: main(): Failed opening ‘…/script/is_login.php?user_name=user’ for inclusion (include_path=’.:/usr/lib/php:/usr/local/lib/php’) in /home/dir/public_html/editalbum/edit/index.php on line 2

Fatal error: Cannot instantiate non-existent class: is_login_class in /home/dir/public_html/editalbum/edit/index.php on line 3

I thank you so much and sorry for the inconvience.

Unless you have a file on your system LITTERALLY named …/script/is_login.php?user_name=$user_name (obviously substitue the username in) then it is going to fail. You are trying to use half file include, half http include. You can include an HTTP but i believe you must do it completely


<?php include("$user_name"); $inc = new is_login_class(); if ($inc->is_login($user_name) == false || $user_name == 'NULL') { ... ?>


As far as debugging and trouble shooting of your code goes, take the first error first and fix that. Many times subsequent errors are the result of initial errors.

Also please read : and before future posts. Pay particular attention to the first one about formatting. It makes things SOOOOOOOOOO much easier to read it in formatted style as opposed to completely justified left code.

The PHP tags are even nicer (not included in the first URL given, as that was added after the fact) as it gives you highlighting appropriate to PHP.


mysql_num_rows will return an error if you do not fetch any data. if you just want a count i would suggest using this…

$db = mysql_connect("host", "user", "pass");
mysql_select_db("photoalbum", $db);
$result = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM album_users", $db), MYSQL_NUM);
echo 'the amount of rows is '.$result[0];

Not true. You do not need to use the FETCH first.

Either way, I think them num_rows question is resolved. Now he is having an error with an include (I think).

Sponsor our Newsletter | Privacy Policy | Terms of Service