RE: Failed MySQL queries

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, April 5, 2006 1:54 pm, Jim Moseby wrote:
>> Rather than using the following, which simply displays a
>> white screen with
>> the die message, is there a way to add the die message to the
>> body of the
>> original form page, such as in a variable, so that I can
>> display the message
>> on the original form page with all the values already filled
>> out, so that
>> all the user has to do is resubmit?
>>
>> 	$result = mysql_query($query)
>> 		or die("User Account could not be
>> created.  Please try again
>> later.");

A better solution, for well-structured code, might be more like this:

<?php
  //A collection of error messages for the end user:
  $messages = array();

  //Set up $connection = mysql_connect(...)
  require 'connect.inc';

  //Do all MySQL queries here:
  $query = "this query will generate an error.  Guaranteed.";
  $result = @mysql_query($query, $connection);
  if ($result === false){
    error_log("mysql_error: " . @mysql_error($connection));
    $messages[] = "Unable to do whatever I meant to do.  Try again
later.";
    $variable1 = array();
  }
  else while ($row = @mysql_fetch_row($result)){
    //store the results in $variable1
  }

  $query = "so will this one.";
  $result = @mysql_query($query, $connection);
  if ($result === false){
    error_log("mysql_error: " . @mysql_error($connection));
    $messages[] = "Unable to do something else.  Try again later.";
    $variable2 = array();
  }
  else while ($row = @mysql_fetch_row($result)){
    //store the results in $variable2
  }
?>
<html>
  <head><title>Better Code</title></head>
  <body>
    <?php if (count($messages)) echo "<p class=\"app_error\">",
implode("</p><p class=\"app_error\">", $messages), "</p>\n";?>
    This is your normal page output here, but written for no data.
    It should gracefully handle empty arrays for $variable1 and
$variable2.
    Which is usually needed even if everything works fine, since an
empty set of results is not unusual, so no "extra" code.
  </body>
</html>

You don't really want to expose internal MySQL errors to your end user.

It's just a Bad Idea on so many levels...

You DO want to have all mysql errors logged so you can figure out what
went wrong.

You DO want to give the user "nice" error messages in a pleasant way.

You can simplify this a bit more with http://php.net/set_error_handler
to and http://php.net/trigger_error, which will also make it easy to
get the file and line number of your problem PHP code into your Apache
log, making debugging about 100 X easier as your application grows.

You could also put the $messages initialization, and $messages output,
into include files, so that they can be included on every page.

Or, for that matter, the $messages output could be part of your
standard include files that pull in your
logo/masthead/navigation/whatever, and then you don't even have to
remember to type the include 'messages.inc' on every page.  That's
what *I* do, personally.

There's more than one way to skin this cat. :-)

YMMV

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux