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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.