essjay_d12 Posted March 27, 2006 Share Posted March 27, 2006 i need to use php to split code into two ways...if there are rows found from the search query then echo 'found'if there are no rows then echo 'not found'thanksd Quote Link to comment https://forums.phpfreaks.com/topic/5925-if-statement-for-no-rows-found/ Share on other sites More sharing options...
obsidian Posted March 27, 2006 Share Posted March 27, 2006 after you run your query, use mysql_num_rows():[code]$sql = mysql_query("SELECT * FROM myTable"); // your query hereif (mysql_num_rows($sql) > 0) echo "Records found!";else echo "No records!";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/5925-if-statement-for-no-rows-found/#findComment-21192 Share on other sites More sharing options...
redbullmarky Posted March 27, 2006 Share Posted March 27, 2006 [!--quoteo(post=358880:date=Mar 27 2006, 02:46 PM:name=Essjay_d12)--][div class=\'quotetop\']QUOTE(Essjay_d12 @ Mar 27 2006, 02:46 PM) [snapback]358880[/snapback][/div][div class=\'quotemain\'][!--quotec--]i need to use php to split code into two ways...if there are rows found from the search query then echo 'found'if there are no rows then echo 'not found'thanksd[/quote][code]if (mysql_num_rows($result)){ echo 'found';}else{ echo 'none found';}[/code][b]EDIT:[/b] whoops beaten again. this post says 'delete me!' Quote Link to comment https://forums.phpfreaks.com/topic/5925-if-statement-for-no-rows-found/#findComment-21193 Share on other sites More sharing options...
essjay_d12 Posted March 27, 2006 Author Share Posted March 27, 2006 i get following errorWarning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Program Files\Apache Group\Apache2\htdocs\dvdreviewer\genreSearch.php on line 271 Quote Link to comment https://forums.phpfreaks.com/topic/5925-if-statement-for-no-rows-found/#findComment-21200 Share on other sites More sharing options...
obsidian Posted March 27, 2006 Share Posted March 27, 2006 [!--quoteo(post=358893:date=Mar 27 2006, 09:28 AM:name=Essjay_d12)--][div class=\'quotetop\']QUOTE(Essjay_d12 @ Mar 27 2006, 09:28 AM) [snapback]358893[/snapback][/div][div class=\'quotemain\'][!--quotec--]i get following errorWarning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Program Files\Apache Group\Apache2\htdocs\dvdreviewer\genreSearch.php on line 271[/quote]that means that most likely, your query is not being completed properly. adjust your query line to have a little error checking:[code]$sql = mysql_query("SELECT * FROM myTable") or die(mysql_error()); // your query hereif (mysql_num_rows($sql) > 0) echo "Records found!";else echo "No records!";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/5925-if-statement-for-no-rows-found/#findComment-21201 Share on other sites More sharing options...
essjay_d12 Posted March 27, 2006 Author Share Posted March 27, 2006 this is how my code is...[code]$query = "SELECT ID_NO, F_NAME, F_DIRECTOR, F_REVIEWER, F_DATETIME, F_TYPE, F_SUMMARY, F_REVIEW, F_SCORE FROM films WHERE F_TYPE LIKE '%$genre%' ORDER BY F_DATETIME LIMIT 1, 10 ";$result = mysql_query($query) or die(mysql_error());if (mysql_num_rows($result > 0)){while ($row = mysql_fetch_array($result)) {$ID_NO = ($row['ID_NO']);$F_NAME = ($row['F_NAME']);$F_DIRECTOR = ($row['F_DIRECTOR']);$F_REVIEWER = ($row['F_REVIEWER']);$F_DATETIME = ($row['F_DATETIME']);$F_TYPE = ($row['F_TYPE']);$F_SUMMARY = ($row['F_SUMMARY']);$F_REVIEW = ($row['F_REVIEW']);$F_SCORE = ($row['F_SCORE']);echo '<tr><td width="100" rowspan="3" valign="top"><img src="images/film/';echo $ID_NO;echo 'a.jpg" width="96" height="140" /></td><td width="227" height="29" valign="top">Film:';echo $F_NAME;echo '</td><td width="86" valign="top"><a href=review.php?id=';echo $ID_NO; echo '> read review </a> </td></tr><tr><td height="31" valign="top">Director: '; echo $F_DIRECTOR; echo '</td><td valign="top">Score: ';echo $F_SCORE; echo '</td></tr><tr><td height="80" colspan="2" valign="top">Summary : ';echo $F_SUMMARY; echo '</td></tr><tr><td height="16" colspan="3" valign="top"><img src="images/siteGraphics/hr.jpg" width="413" height="11" alt="----------------------------------------------" /></td></tr>'; } }else{echo 'No film reviews in this Genre';}?>[/code]it worked fine before i put the if statement in - only problem if no rows were found it just displayed a blank page and i need a message there, so need the if statement Quote Link to comment https://forums.phpfreaks.com/topic/5925-if-statement-for-no-rows-found/#findComment-21204 Share on other sites More sharing options...
redbullmarky Posted March 27, 2006 Share Posted March 27, 2006 your problem as far as i can see is here:[code]if (mysql_num_rows($result > 0)){[/code]what PHP does is evaluates the equation in the brackets first ($result>0) and then runs mysql_num_rows on it. $result>0 will return true or false, and mysql_num_rows(true) or mysql_num_rows(false) will both return errors.what you need:[code]if (mysql_num_rows($result) > 0) {[/code]hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/5925-if-statement-for-no-rows-found/#findComment-21219 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.