Re: Understanding If conditional statements

From: Date: Sun, 14 Apr 2002 23:34:31 +0000
Subject: Re: Understanding If conditional statements
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-92945@lists.php.net to get a copy of this message
Hi, I've rewritten the code to: // Sends the Query $sql_result = mysql_query($sql); if (!$sql_result) { echo "Can't execute $sql " . mysql_error(); exit; } if (mysql_num_rows($sql_result) == 0) { $option_block .= "Sorry, your search has resulted in 0 records. Please try again. \n"; } // organizes data in an orderly manner (ie bulleted area) while ($row = mysql_fetch_array($sql_result)) { $advocateID = $row["advocateID"]; $esc_fname = $row["FNAME"]; $esc_lname = $row["LNAME"]; $esc_firm = $row["FIRM"]; $esc_city = $row["CITY"]; $esc_province = $row["PROVINCE"]; $esc_area = $row["AREA"]; .... and it works beautifully! Thanks for pointing me to the clue! "David Robley" <robleyd@paradise.net.nz> wrote in message news:MPG.1724d50de2b07ece9896f4@news.php.net... > In article <20020414225858.8717.qmail@pb1.pair.com>, junkit@lmlweb.com > says... > > I'm trying to get my code to print out a "sorry - no results" message if > > there is no match. I've read the If statement in the PHP manual > > (http://www.php.net/manual/ro/control-structures.php#control-structures.if) > > > > and > > > > I think my code structure may be wrong.. am I wrong to nest it this way in > > the following code? If so, where should I be putting the " if $sql returns 0 > > records, then print.." code? > > > > while ($row = mysql_fetch_array($sql_result)) { > > if ($sql_result = 0) { > > $option_block .= "Sorry, your search has resulted in 0 records. Please try > > again. \n"; > > } > > else { > > Firstly, $sql_result insn't the value to check and secondly you should do > it before you start the while loop. > > Do your query to the DB, then check mysql_num_rows. If it is zero, print > the error message; otherwise go into the while loop to display your > results. > > -- > David Robley > Temporary Kiwi! > > Quod subigo farinam

« previous php.general (#92945) next »