if statement help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

anticore
Forum Newbie
Posts: 21
Joined: Sat Mar 11, 2006 11:37 pm

if statement help

Post 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...
anticore
Forum Newbie
Posts: 21
Joined: Sat Mar 11, 2006 11:37 pm

Post 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"; 
	}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

in your new code, the else is being associated with the while() which is not legal.
anticore
Forum Newbie
Posts: 21
Joined: Sat Mar 11, 2006 11:37 pm

Post by anticore »

oh. :roll:
it should go with thte second if then right ?
anticore
Forum Newbie
Posts: 21
Joined: Sat Mar 11, 2006 11:37 pm

Post by anticore »

what does $r return if nothing isretrieved?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
anticore
Forum Newbie
Posts: 21
Joined: Sat Mar 11, 2006 11:37 pm

Post by anticore »

empty as in " " or 0 or null ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

empty as in no records that matched the criteria were found.
anticore
Forum Newbie
Posts: 21
Joined: Sat Mar 11, 2006 11:37 pm

Post 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";
   }
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I told you, a resource.
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Post 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.
anticore
Forum Newbie
Posts: 21
Joined: Sat Mar 11, 2006 11:37 pm

Post by anticore »

Code: Select all

if empty($r)
  { 
     print " The Message";
  }
would that work too ?
asgerhallas
Forum Commoner
Posts: 80
Joined: Tue Mar 14, 2006 11:11 am
Location: Århus, Denmark

Post by asgerhallas »

no... wouldn't work - if $r is a ressource it's not empty.
noobies version is the correct one...
Post Reply