Jump to content


Photo

Fetching results


  • Please log in to reply
1 reply to this topic

#1 Mr Chris

Mr Chris
  • Members
  • PipPipPip
  • Advanced Member
  • 336 posts

Posted 20 July 2006 - 09:37 AM

H Guys,

I’m trying to build a query whereby I return many results from a database.

Now I’ve done it so it returns one result:

<?
$SQL = "SELECT * FROM cms_stories WHERE (headline LIKE '%$term_one%' 
OR headline LIKE '%$term_two%' OR body_text LIKE '%$term_one%' OR 
body_text LIKE '%$term_two%') AND story_id != $story_id"; 
$result = mysql_query($SQL) OR die(mysql_error()); 
$row = mysql_fetch_array($result); 
echo "<a href='story.php?story_id={$row[story_id]}'>{$row[headline]}</a>"; 
?>


But when I try and make it return many rows of results it returns the error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource


<?

$query = "SELECT * FROM cms_stories WHERE (headline LIKE '%$term_one%' 
OR headline LIKE '%$term_two%' OR body_text LIKE '%$term_one%' OR 
body_text LIKE '%$term_two%') AND story_id != $story_id"; 
$result = mysql_query($SQL) OR die(mysql_error()); 

if (mysql_num_rows($query) <= 0) { 
echo ("<DIV ALIGN=\"CENTER\">Sorry, there are no related stories</div>"); 
} 

else {
// If there is a result!!; 
if (mysql_num_rows($query) > 0) { 
echo ("Results"); 
}
}
while($rows = mysql_fetch_assoc($query))   { 
echo "<a href='story.php?story_id={$row[story_id]}'>{$row[headline]}</a>"; 
} 
echo "</table>"; 

?>

Can anyone please help?

Thanks

Chris


#2 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 20 July 2006 - 09:59 AM

mysql_num_rows must be performed on the variable where the query was actually made, so adjust it to:
mysql_num_rows($result).

Also, in this part:
while($rows = mysql_fetch_assoc($query))  {
echo "<a href='story.php?story_id={$row[story_id]}'>{$row[headline]}</a>";
}
change $rows = to $row =






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users