mysql_num_rows() error

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Indium
Forum Newbie
Posts: 15
Joined: Tue Jul 02, 2002 8:30 pm

mysql_num_rows() error

Post 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>
samscripts
Forum Commoner
Posts: 57
Joined: Tue Apr 23, 2002 4:34 pm
Location: London, UK

Post 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
Indium
Forum Newbie
Posts: 15
Joined: Tue Jul 02, 2002 8:30 pm

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply