...

View Full Version : mysql_num_rows(): supplied argument is not a valid



whatg
06-21-2010, 08:30 PM
Ok so I'm not completely new to php. Have done some basic scripts. Thought I would try and be clever by displaying information from one of my tables in mysql. That didn't go quite to plan.

When I ran the script I got this error:"mysql_num_rows(): supplied argument is not a valid"

Apparently the most common mistake is connecting to the wrong table, I have checked and re-checked this and I sure that I'm (also checked upper/lowercase ). I also ran a simple script to just connect to the database, that works.

Any help would be great, thanks. :D

This is my code:


<?php

//set variables to connect
$host='localhost';
$user='grahame2';
$pass='Q2JKWP@F';
$db='grahame2_test';


//connect to mysql
mysql_connect($host,$user,$pass,$db) or die("Unable to connect to the database");

$query="SELECT * FROM family";

$result=mysql_query($query) or die("Ivalid query");


$num=mysql_num_rows($result);



$i=0;
while($i<num){

$forename=mysql_result($result,$i,"forename");
$surename=mysql_result($result,$i,"surname");
$age=mysql_result($result,$i,"age");


echo "$forename,$surname,$age";

$i++;

}

?>

Fumigator
06-21-2010, 09:14 PM
Actually the most common mistake is one you've made, and that is you are not checking the query call to see if it worked. Report any errors you find and you'll likely find the problem quickly thereafter.



$result=mysql_query($query);

//check the result
if (!$result) {
die("Query error! text is $query<br>error is ".mysql_error());
}
$num=mysql_num_rows($result);


In case you're interested in the cause of the error you're seeing, it's simply telling you the mysql resource you passed to mysql_num_rows() isn't actually a mysql resource. Your $result variable will be a mysql resource if your call to mysql_query() works. If it fails, it will not be a valid mysql resource (it will instead be a boolean set to FALSE) and if you then pass it to a function that is expecting a mysql resource, you'll get the error you got.

whatg
06-21-2010, 09:31 PM
Hi,

Thanks for your post, really helped. Ok so the mistake I was making was not connecting to the database- which I resolved quite quickly (I think).

But now I just see a blank page when I run the script. Here is the code revised:


<?php

//set variables to connect
$host='localhost';
$user='grahame2';
$pass='Q2JKWP@F';
$db='grahame2_test';


//connect to mysql
mysql_connect($host,$user,$pass);

@mysql_select_db($db) or die( "Unable to select database");

$query="SELECT * FROM family";

$result=mysql_query($query);

if (!$result){

die("Query error! text $query<br> error is".mysql_error());


}


$num=mysql_num_rows($result);

mysql_close();


$i=0;
while($i<num){

$forename=mysql_result($result,$i,"forename");
$surename=mysql_result($result,$i,"surname");
$age=mysql_result($result,$i,"age");


echo "$forename,$surname,$age";

$i++;

}






?>

Fumigator
06-21-2010, 10:13 PM
while($i<num){

You mean $num there, right? PHP is interpreting "num" as an undefined constant and your error reporting must be turned down low enough to not report it.

whatg
06-21-2010, 10:30 PM
Thanks, can't believe I didn't see that! Think I was expecting it to be something fundamentally wrong :-D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum