...

View Full Version : mysql_num_rows not working



masterofollies
12-04-2008, 03:41 PM
Anyone have a clue why this doesn't work? and why it processes the following error?


$checkquery = mysql_query("SELECT * FROM titles WHERE id>'0' ");

$num_rows = mysql_num_rows($checkquery); //Line 113
if ($num_rows = 0)
{
echo '<b>No Titles Found.</b><br><br><br>';
}

The error below

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/content/y/a/n/yankeefaninkc/html/argyle2/admin.php on line 113

che_anj
12-04-2008, 03:48 PM
$num_rows=mysql_num_rows($checkquery);
if(mysql_num_rows($num_rows) < 1 ){
echo '<b>No Titles Found.</b><br><br><br>';
}

abduraooft
12-04-2008, 03:57 PM
As always we say, add proper error checks to your query.


$checkquery = mysql_query("SELECT * FROM titles WHERE id>'0' "); should be

$checkquery = mysql_query("SELECT * FROM titles WHERE id>'0' ") or die(mysql_error());

PS:When comparing against a numeric type filed, avoid using quotes around values.

masterofollies
12-04-2008, 05:04 PM
Hi,

I am still getting the same mysql_num_rows error. Here is what I put in


function etitles() {

$checkquery = mysql_query("SELECT * FROM titles WHERE titleid>'0' ") or die(mysql_error());;


echo '<hr><center><h2>Administrator Control Panel</h2></center><hr><br><br>';
echo 'Select the user you wish to edit.<br><br>';
$num_rows=mysql_num_rows($checkquery);
if(mysql_num_rows($num_rows) < 1 ){
echo '<b>No Titles Found.</b><br><br><br>';
}

abduraooft
12-04-2008, 05:11 PM
$num_rows=mysql_num_rows($checkquery);
if(mysql_num_rows($num_rows) < 1 ){
echo '<b>No Titles Found.</b><br><br><br>';
} What's that?

masterofollies
12-04-2008, 06:30 PM
Dunno thats the code che_anj posted.

All I want it to do is check if there are any records in a table, and if not, post there isn't any.

JohnDubya
12-04-2008, 06:34 PM
Hi,

I am still getting the same mysql_num_rows error. Here is what I put in


function etitles() {

$checkquery = mysql_query("SELECT * FROM titles WHERE titleid>'0' ") or die(mysql_error());;


echo '<hr><center><h2>Administrator Control Panel</h2></center><hr><br><br>';
echo 'Select the user you wish to edit.<br><br>';
$num_rows=mysql_num_rows($checkquery);
if(mysql_num_rows($num_rows) < 1 ){
echo '<b>No Titles Found.</b><br><br><br>';
}

If you'll notice, you are actually performing the mysql_num_rows() function twice, which is where your error is coming from. You first use it and place the return value in the variable $num_rows. Then, in the if statement, you run mysql_num_rows() again (on the variable you just set). Here's how it should be:



$num_rows = mysql_num_rows($checkquery); //$num_rows now contains the number of rows from the query
if ($num_rows < 1 ) {
echo '<b>No Titles Found.</b><br><br><br>';
}

masterofollies
12-04-2008, 07:30 PM
Ah I see, this is the first time I've used mysql_num_rows so it's a learning experience. Thanks

abduraooft
12-04-2008, 07:45 PM
Always check the manual (http://php.net/mysql_num_rows), before using any new function. You'd get plenty of examples and related stuffs from there.

masterofollies
12-05-2008, 03:40 AM
I actually read that before posting, but I couldn't find an example like mine. Well I am sure there was but I didn't understand it. Maybe I should play with a few of the examples and learn.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum