Jump to content

Recommended Posts

I would like to find a better way of stopping this script from trying to list results if none exist

 

at the moment I use this

  if(!$result) die(mysql_error()); 
     $err = mysql_num_rows($result); 
       if($err == 0) die("No matches met your criteria.");

 

but it stop the rest of the page displaying.  Is there a better way of displaying the rest of the page without trying to create an empty table?

 

$result = mysql_query("Select count(*) from carshare") 
or die (mysql_error()); 
$numrows = mysql_fetch_row($result); 
  
if(isset($_GET['pagenum'])?$page = $_GET['pagenum']:$page = 1); 
$entries_per_page = 45;   

$total_pages = ceil($numrows[0]/$entries_per_page); 
$offset = (($page * $entries_per_page) - $entries_per_page); 

$result = mysql_query("SELECT `id`,`user`,`seats_available`,`start_street`,`start_postcode`,`start_lat`,`start_long`,
 `end_street`,`end_postcode`,`end_lat`,`end_long`,`depart_time` FROM carshare ORDER BY `id` LIMIT  
                       $offset,$entries_per_page");
  if(!$result) die(mysql_error()); 
     $err = mysql_num_rows($result); 
       if($err == 0) die("No matches met your criteria."); 

function DisplayURL($url) {
    if (strlen($url) > 30) return(substr($url,0,30) . '...');
    else return($url);
}

echo "<table id='MyTable' class='style3'>\n";
echo "<tr><th colspan='2'> </th><th colspan='2'>Start of Route</th><th colspan='2'>End of Route</th></tr>
<tr><th>Seats</th><th>Depart</th><th>Street</th><th>Postcode</th><th>Street</th><th>Postcode</th></tr>\n";

$z = 0;

while($row=mysql_fetch_array($result))
    {

    $seats = htmlentities($row['seats_available']);
    $start_street = htmlentities($row['start_street']);
    $start_postcode = htmlentities($row['start_postcode']);
    $end_street = htmlentities($row['end_street']);
    $end_postcode = htmlentities($row['end_postcode']);
    $depart = htmlentities($row['depart_time']);

    if($z % 2==0)
    { //this means if there is a remainder
        echo "<tr class='yellow'>\n";
$z++;
    }
    	else
    { //if there isn't a remainder we will do the else
        echo "<tr class='white'>\n";
$z++;
    }
    	echo "<td>".$seats."</td><td>".$depart."</td><td>".$start_street."</td><td>".$start_postcode."</td><td>".$end_street."</td><td>".$end_postcode."</td>\n";
    	echo "</tr>\n";
    }
//now let's close the table and be done with it
echo "</table>\n";

Link to comment
https://forums.phpfreaks.com/topic/95149-is-there-a-better-way-than-die/
Share on other sites

It's not that there are no records to display. Using die in that fashion means there was a critical error when trying to run the query. But, you can (and should) take an approach that exits the script gracefully.

 

Note: You should create your queries as variables so you can echo them to the page when there is an error:

 

Example:

<?php

$query = "Select count(*) from carshare";
$result = mysql_query($query);

if (!$result) {

  echo "The query:<br>$query<br><br>";
  echo "failed with the following error:<br>".mysql_error();

} else {

  //display the results

}

//Continue with rest of page

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.