Page 1 of 2

if statement help

Posted: Sat Mar 18, 2006 12:44 am
by anticore

Code: Select all

if (isset($cat)){
	if ($r = mysql_query($query1) or die(mysql_error()))
	{ 
//print 

		while ($row = mysql_fetch_array ($r)) 
		{ 
    	print "<a href=\"list.php?id={$row['id']}\"><IMG SRC=\"{$row['smpic']}\"  ALT=\"{$row['name']}\"></a>";
		print "{$row['name']}"; 
 		}
	}else {
		print " No Rides Here! <br> If you would like to have your ride added to this section email  us"; 
	} 
}
The No Rides Here blah blah only prints if theres no var in the url even though i put it under the mysql if statement...

Posted: Sat Mar 18, 2006 1:04 am
by anticore
tried changing something an now i got a parse error on line 27 which is "}else{"

Code: Select all

while ($row = mysql_fetch_array ($r)) 
		{ 
    	print "<a href=\"list.php?id={$row['id']}\"><IMG SRC=\"{$row['smpic']}\"  ALT=\"{$row['name']}\"></a>";
		print "{$row['name']}"; 
 		}else {
		print " No Rides Here! <br> If you would like to have your ride added to this section email us"; 
	}

Posted: Sat Mar 18, 2006 1:07 am
by feyd
in your new code, the else is being associated with the while() which is not legal.

Posted: Sat Mar 18, 2006 1:09 am
by anticore
oh. :roll:
it should go with thte second if then right ?

Posted: Sat Mar 18, 2006 1:23 am
by anticore
what does $r return if nothing isretrieved?

Posted: Sat Mar 18, 2006 1:28 am
by feyd
If the result set is empty, it's a resource. If the query failed, it'll be false and the die() will be run.

Posted: Sat Mar 18, 2006 1:29 am
by anticore
empty as in " " or 0 or null ?

Posted: Sat Mar 18, 2006 1:31 am
by feyd
empty as in no records that matched the criteria were found.

Posted: Sat Mar 18, 2006 1:34 am
by anticore
i understand that, just what get returned to the variable? nothing at all so it set to $r="" ?

Code: Select all

if ($r="")
   {
       print "this";
   }

Posted: Sat Mar 18, 2006 1:45 am
by feyd
I told you, a resource.

Posted: Sat Mar 18, 2006 11:07 am
by Noobie
I have a similar issue - mainly because I would like a message echoed if the result set is empty. Is there a way of doing that?

Posted: Sat Mar 18, 2006 11:17 am
by feyd
Have a look through the functions MySQL support exposes. I'll give you a hint, it has something to do with rows.

http://php.net/ref.mysql

Posted: Sat Mar 18, 2006 12:21 pm
by Noobie
Not fair (stamps feet) you always make me find it myself! What's wrong with a little spoon-feeding once in a while eh? :D

I don't know if it's "right" but it works - I included this:

Code: Select all

if (mysql_num_rows($result) == 0) {
      echo "<p>" . "Nothing to see here, move along now" .  "</p>";
      exit;
}
Thanks for pointing me in the right direction.

Posted: Sat Mar 18, 2006 12:31 pm
by anticore

Code: Select all

if empty($r)
  { 
     print " The Message";
  }
would that work too ?

Posted: Sat Mar 18, 2006 1:00 pm
by asgerhallas
no... wouldn't work - if $r is a ressource it's not empty.
noobies version is the correct one...