NYCPHP Meetup

NYPHP.org

[nycphp-talk] Help with error statement

Rob Marscher rmarscher at beaffinitive.com
Thu Nov 16 11:35:29 EST 2006


You want to use mysql_num_rows - 
http://us3.php.net/manual/en/function.mysql-num-rows.php - after your 
query.  You can then do an if statement to check if mysql_num_rows != 0 
before doing the while loop:

$sql_events = mysql_query("SELECT * FROM area36 WHERE zipcode  
='$key'") or die (mysql_error());
if (mysql_num_rows() !== 0)
{
  while ($row = mysql_fetch_array($sql_events)) {
    // stuff in here
  }
}
else
{
  // error - no records found
}



Paul wrote:
> I have the start of a zipcode radius locator.  It seems to work but  
> the problem I am having is creating an error statement if no  
> locations are found.  At first it made sense to check for retuned  
> zipcode values.  The problem being one zipcode will always be  
> returned the one that is used for the search.  So my thinking would  
> be to uses the retuned query that actually looks for the locations :  
> $sql_events = mysql_query("SELECT * FROM area36 WHERE zipcode  
> ='$key'") or die (mysql_error());  .  However anything I try seems to  
> loop and create ugly things.
>
> I am a rank beginner far better photographer than coder so talk  
> simple.  My brain is also a little tired after seeing Ziggy Marley  
> last night as you might expect, so I apologize up front for my  
> sluggish brain.
>
> Paul Guba
>
>
>
> <?php
> require_once('zipcode.class.php');
> mysql_connect("localhost", "something", "something") or die 
> (mysql_error());
> mysql_select_db("ziplocator") or die(mysql_error());
> echo "<table>";
> echo "<center><tr> <th>Dist</th><th>Day</th> <th>Town</th><th>Time</ 
> th><th>Place</th><th>Street</th><th>Type</th><th>Format</th><th>HCA</ 
> th><th>NS</th> </tr>";
>
> // colors of returned rows uses style sheet
>
> $color1 = "meet";
> $color2 = "altmeet";
> $row_count = 0;
> // find all zipcodes values within zip , mile radius use  
> zipcode.class.php written by: Micah Carrick
> $z = new zipcode_class;
> $zips = $z->get_zips_in_range('07732', '10',  
> _ZIPS_SORT_BY_DISTANCE_ASC, true);
> foreach ($zips as $key => $value)
> // for error not working presently
> {
> if(isset($key) && count($key) > 0)
> //find all locations within range using returned zipcode values
> {
> $sql_events = mysql_query("SELECT * FROM area36 WHERE zipcode  
> ='$key'") or die (mysql_error());
> while ($row = mysql_fetch_array($sql_events)) {
> 	$dist = $row['dist'];
>      $day = $row['day'];
>      $town = $row['town'];
> 	$time = $row['time'];
> 	$place = $row['place'];
> 	$street = $row['street'];
> 	$type = $row['type'];
> 	$Special = $row['Special'];
> 	$hca = $row['hca'];
> 	$ns = $row['ns'];
>
>      //row count for alternating colors
>
>      $row_color = ($row_count % 2) ? $color1 : $color2;
>
>      //create tables for data row loop
>
>      echo "<tr>
> 	<td class='$row_color'>$dist</td>
>      <td class='$row_color'>$day</td>
>      <td class='$row_color'>$town</a></td>
> 	<td class='$row_color'>$time</a></td>
> 	<td class='$row_color'>$place</a></td>
> 	<td class='$row_color'>$street</a></td>
> 	<td class='$row_color'>$type</a></td>
> 	<td class='$row_color'>$Special</a></td>
> 	<td class='$row_color'>$hca</a></td>
> 	<td class='$row_color'>$ns</a></td>
>      </tr>";
>
>      // Add 1 to the row count
>
>      $row_count++;
> }
> }
> // error statement if no locations are found within search radius
> else {
> echo 'Sorry, your search returned no locations.  Try using a larger  
> radius to search.';
> }
> }
> // end table
>
> echo "</table>";
> ?>
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
>
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php
>
>   

-- 
Rob Marscher
Software Engineer
rmarscher at beaffinitive.com
212.684.9100x17




More information about the talk mailing list