Jump to content

Fetching results


Mr Chris

Recommended Posts

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:

[code=php:0]
<?
$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>";
?>
[/code]


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

[b] Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource[/b]

[code=php:0]

<?

$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>";

?>
[/code]

Can anyone please help?

Thanks

Chris
Link to comment
https://forums.phpfreaks.com/topic/15120-fetching-results/
Share on other sites

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 =

Link to comment
https://forums.phpfreaks.com/topic/15120-fetching-results/#findComment-60906
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.