mysql_num_rows

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

mysql_num_rows

Post by nigma »

I am having trouble with this code:

Code: Select all

$result = mysql_query("select * from cs15replies where subject='$subject'");
$replies = mysql_num_rows($result);
if ($replies >= 1)
{
  while ($row = mysql_fetch_array($rep))
  {
     $lastReply = $rrowї'author'];
  }
}
if ($lastReply == "") { $lastReply = "No Replies"; }
This code snippet is run for every row in the cs15posts table. Sometimes everything works out fine, and sometimes I get this message:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in filname.php on line [linenumber]

Any ideas why the code is inconsistent in producing the same result?
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Re: mysql_num_rows

Post by Kriek »

That error means that the query is invalid syntax. Note you should NOT be using asterisks in your query as a list of comma separated values is going to be much faster. You can debug your script with a combination of mysql_error(), mysql_errno() and echoing the query back to the page. The following was tested locally and was found to run flawlessly.

Code: Select all

<?php
    $query = "SELECT author,subject FROM cs15replies WHERE subject='$subject'";
    $result = mysql_query($query) or die (mysql_errno() . ': ' . mysql_error() . '<BR>' . $query);
    $replies = mysql_num_rows($result) or die (mysql_errno() . ': ' . mysql_error());
    if ($replies >= 1) {
        while ($row = mysql_fetch_array($result)) {
            $lastReply = $row['author'];
        }
    }
    if (empty($lastReply)) {
        $lastReply = 'No Replies';
    }
    echo $lastReply;
?>
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Tried that and all I get is "0:"
then nothing is printed cause program died.
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

Code: Select all

<?php
$result = mysql_query("select * from cs15replies where subject='$subject'"); 
$replies = mysql_num_rows($result); 
if ($replies >= 1) 
{ 
  while ($row = mysql_fetch_array($result)) 
  { 
     $lastReply = $row['author']; 
  } 
}
else
{
$lastReply = "No Replies";
}
?>
andrewt
Forum Newbie
Posts: 11
Joined: Fri Aug 08, 2003 8:18 am
Location: Sydney, Australia

Empty Set.

Post by andrewt »

Hi ...

try echoing $subject...
and see if the data is legit.


Andrew
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

Try it now, again tested locally.

Code: Select all

<?php
    $query = "SELECT author,subject FROM cs15replies WHERE subject='$subject'";
    $result = mysql_query($query) or die (mysql_error());
    $replies = mysql_num_rows($result) or die (mysql_error());
    if ($replies >= 1) {
        while ($row = mysql_fetch_array($result)) {
            $lastReply = $row['author'];
        }
    }
    if (empty($lastReply)) {
        $lastReply = 'No Replies';
    }
    echo $lastReply;
?>
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Thanks kriek. I was looking through some of my past posts and thought "ahh.... <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>! I didn't tell this guy that he helped me out".
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

No problem, glad I could be of some assistance to you ;)
derek
Forum Newbie
Posts: 17
Joined: Sat Aug 16, 2003 11:31 am

Post by derek »

Code: Select all

<?php
$ans="";
$sql_query = "select * from cs15replies where subject='$subject'";
$result = mysql_query($sql_query);

if(mysql_num_rows($result)) 
{
	while ($row = mysql_fetch_array($result)) 
	{ 
		$ans = $row['name']; 
	}	
}
else
	$ans = "No Replies";

echo $ans;




I hope this code is also helpful to you.
?>
Last edited by derek on Mon Aug 18, 2003 5:24 am, edited 2 times in total.
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

Use [syntax=php]tags rather than[/syntax][syntax=php]tags when you're posting a php code snippet. It really helps us follow the code, and sometimes it even points out syntax errors via the code highlighting that people otherwise can't detect.[/syntax]
derek
Forum Newbie
Posts: 17
Joined: Sat Aug 16, 2003 11:31 am

Post by derek »

THANKS FOR YOUR ADVICE qartis :wink:
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post by Coco »

i also get that error if the select doesnt return any rows...
i get round this by doing the following:

Code: Select all

<?php

$count = 0;
$count = @mysql_num_rows($result);
if($count==0)
{
}

?>
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

For the sake of continuity, you'll see most php would be written like..

Code: Select all

if (@mysql_num_rows($result)>0){
    while($row = mysql_fetch_assoc($result)){
        /* Polka time */
    }
} else {
    /* No rows returned */
}
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Is this even better?

Code: Select all

if ($result)
&#123;
  if (@mysql_num_rows($result) > 0)
  &#123;
    while (mysql_fetch_array($result))
    &#123;
      // Code here
    &#125;
  &#125;
  else
  &#123;
    //No rows returned
  &#125;
&#125;
else
&#123;
  // Result not valid
&#125;
Is that even better?
Post Reply