Jump to content


Photo

if statement for no rows found


  • Please log in to reply
6 replies to this topic

#1 essjay_d12

essjay_d12
  • Members
  • PipPipPip
  • Advanced Member
  • 121 posts

Posted 27 March 2006 - 01:46 PM

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

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 27 March 2006 - 01:59 PM

after you run your query, use mysql_num_rows():
$sql = mysql_query("SELECT * FROM myTable"); // your query here
if (mysql_num_rows($sql) > 0)
  echo "Records found!";
else
  echo "No records!";

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 27 March 2006 - 01:59 PM

[!--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) View Post[/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]


if (mysql_num_rows($result))
{
   echo 'found';
}
else
{
   echo 'none found';
}

EDIT: whoops beaten again. this post says 'delete me!'
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#4 essjay_d12

essjay_d12
  • Members
  • PipPipPip
  • Advanced Member
  • 121 posts

Posted 27 March 2006 - 02:28 PM

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

#5 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 27 March 2006 - 02:30 PM

[!--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) View Post[/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:
$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!";


You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#6 essjay_d12

essjay_d12
  • Members
  • PipPipPip
  • Advanced Member
  • 121 posts

Posted 27 March 2006 - 02:33 PM

this is how my code is...

$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';
}
?>

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

#7 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 27 March 2006 - 03:06 PM

your problem as far as i can see is here:

if (mysql_num_rows($result > 0)){

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:

if (mysql_num_rows($result) > 0) {

hope that helps

"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users