Page 1 of 1
mysql_num_rows
Posted: Sun Aug 10, 2003 2:46 pm
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?
Re: mysql_num_rows
Posted: Sun Aug 10, 2003 3:29 pm
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;
?>
Posted: Sun Aug 10, 2003 4:34 pm
by nigma
Tried that and all I get is "0:"
then nothing is printed cause program died.
Posted: Sun Aug 10, 2003 4:43 pm
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";
}
?>
Empty Set.
Posted: Sun Aug 10, 2003 4:57 pm
by andrewt
Hi ...
try echoing $subject...
and see if the data is legit.
Andrew
Posted: Sun Aug 10, 2003 6:41 pm
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;
?>
Posted: Sun Aug 17, 2003 3:41 pm
by nigma
Thanks kriek. I was looking through some of my past posts and thought "ahh.... <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span>! I didn't tell this guy that he helped me out".
Posted: Sun Aug 17, 2003 10:27 pm
by Kriek
No problem, glad I could be of some assistance to you

Posted: Mon Aug 18, 2003 2:33 am
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.
?>
Posted: Mon Aug 18, 2003 2:43 am
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]
Posted: Mon Aug 18, 2003 5:28 am
by derek
THANKS FOR YOUR ADVICE
qartis 
Posted: Mon Aug 18, 2003 6:48 am
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)
{
}
?>
Posted: Mon Aug 18, 2003 2:09 pm
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 */
}
Posted: Mon Aug 18, 2003 2:20 pm
by nigma
Is this even better?
Code: Select all
if ($result)
{
if (@mysql_num_rows($result) > 0)
{
while (mysql_fetch_array($result))
{
// Code here
}
}
else
{
//No rows returned
}
}
else
{
// Result not valid
}
Is that even better?