Mr Chris Posted July 20, 2006 Share Posted July 20, 2006 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?ThanksChris Quote Link to comment https://forums.phpfreaks.com/topic/15120-fetching-results/ Share on other sites More sharing options...
GingerRobot Posted July 20, 2006 Share Posted July 20, 2006 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 = Quote Link to comment https://forums.phpfreaks.com/topic/15120-fetching-results/#findComment-60906 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.