Page 1 of 1

mysql_num_rows() error

Posted: Sat Jul 20, 2002 7:20 pm
by Indium
I get the following warning in my browser:

"mysql_num_rows(): supplied argument is not a valid MySQL result resource in d:\inetpub\wwwroot\test.php"

I get a similar message with mysql_num_row($result) if I remove the If statement. Any ideas?

Here is the code:

Code: Select all

<?
// login to database
require_once ('db_login.php');
?>

<?
// generate and execute query
$sql = "SELECT id, Entry, Description FROM A";
$result = $db->query($sql) or die ("Error in query: $sql. " . mysql_error());
$db->commit();

// if records present
if (mysql_num_rows($result) > 0)
&#123;
	// iterate through result set
	while ($row = mysql_fetch_row($result))
	&#123;
	?>
	<? echo $row->Entry; ?>
	<? echo $row->Description; ?>
	<br>
	<a href="edit.php?id=<? echo $row->id; ?>">Edit</a> |
	<a href="delete.php?id=<? echo $row->id; ?>">Delete</a>
	<?
	&#125;
&#125;
// if no records present display message
else
&#123;
?>
<p>No entries currently available</p>
<?
&#125;

// close connection
// mysql_close($dsn);
?>

<a href="add.php">Add Entry</a>

Posted: Sat Jul 20, 2002 7:36 pm
by samscripts
HI, the following message:

Code: Select all

mysql_num_rows(): supplied argument is not a valid MySQL result resource in d:\inetpub\wwwroot\test.php
means that something went wrong with the query. I don't know what the db class you are using is so I can't tell what's going wrong.

Try placing a call to mysql_error() after the $db->commit statement like this:

Code: Select all

$db->commit();
echo mysql_error();
That should give you a more meaningful error message.

You could also try calling the mysql_query() function directly with the $sql, rather than using the $db class - like this:

Code: Select all

$result = mysql_query($sql) or die(mysql_error());
This should definately tell you what the error is.

hope something here helps, Sam

Posted: Sat Jul 20, 2002 10:56 pm
by Indium
You could also try calling the mysql_query() function directly with the $sql, rather than using the $db class - like this:

Code:
$result = mysql_query($sql) or die(mysql_error());
That cured the problem - but I use the db class (php\pear\db.php) in a similar file and it queries the database just fine - can't for the life of me figure out why this code would be any different.

Also, I had to used $row[0]; instead of $row->id; in the following two lines:

Code: Select all

<a href="edit.php?id=<? echo $row->id; ?>">Edit</a> | 
   <a href="delete.php?id=<? echo $row->id; ?>">Delete</a>
in order to get the id number appended to the url. Do you know why?

Thanks.

Posted: Sun Jul 21, 2002 6:07 am
by twigletmac
Changing [url=http:www.php.net/mysql_fetch_row]mysql_fetch_row()[/url] to [url=http:www.php.net/mysql_fetch_assoc]mysql_fetch_assoc()[/url] should help.

Mac