Jump to content

Archived

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

essjay_d12

if statement for no rows found

Recommended Posts

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'

thanks

d

Share this post


Link to post
Share on other sites
after you run your query, use mysql_num_rows():
[code]
$sql = mysql_query("SELECT * FROM myTable"); // your query here
if (mysql_num_rows($sql) > 0)
  echo "Records found!";
else
  echo "No records!";
[/code]

Share this post


Link to post
Share on other sites
[!--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'

thanks

d
[/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!'

Share this post


Link to post
Share on other sites
i get following error

Warning: 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

Share this post


Link to post
Share on other sites
[!--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 error

Warning: 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 here
if (mysql_num_rows($sql) > 0)
  echo "Records found!";
else
  echo "No records!";

[/code]

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

×

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.