p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: bringing up a friendly error message instead of the SQL Error


Message #1 by hakawati@g... on Tue, 4 Feb 2003 13:57:47
Hello,

I have a website where I get an SQL error message:
for example:

Warning: Supplied argument is not a valid MySQL result resource in /test/test1/test.php on line 229

Warning: Variable passed to each() is not an array or object  

This error is caused by incomplete data in the database.

I want to bring up an error message like:

"Your data in the db is not complete. Please contact "blabla""

How can I do this??
Message #2 by "Nikolai Devereaux" <yomama@u...> on Tue, 4 Feb 2003 10:51:13 -0800
> I have a website where I get an SQL error message:
> for example:
>
> Warning: Supplied argument is not a valid MySQL result resource in
> /test/test1/test.php on line 229
>
> Warning: Variable passed to each() is not an array or object
>
> This error is caused by incomplete data in the database.

I doubt it.  If the database tables don't contain enough data to match at
least one row for a SELECT query, then you will STILL get a valid result
resource identifier from mysql_query(), that resource id will just identify an
empty result rowset.

Generally speaking, the result resource returned by mysql_query() is only an
invalid resource if there was a problem executing the query.  90% of the time,
this is due to a syntax error of some sort in the query itself.  Other times
it may be because there isn't a valid connection open to mysql or something
like that.

> I want to bring up an error message like:
>
> "Your data in the db is not complete. Please contact "blabla""
>
> How can I do this??

The simple thing is:

$result = mysql_query($query);

if(!$result)
{
   echo "There was an error running your query.\n"
      . "Query: $query\n"
      . "Error: " . mysql_error() . "\n";
}
else if(mysql_num_rows($result) == 0)
{
   echo "No result rows matched.";
}
else
{

}



Also, when dealing with array result sets from db queries, it's best to
perform validity checks on your arrays.  This will prevent the error message
you saw about passing a non-array variable to each().

while($row = mysql_fetch_array($result))
{
   // $row is an array inside this loop.
}


If $row is not an array, it will be FALSE, so the loop body won't execute.



Take care,

Nik


Message #3 by "Gellings, C.O." <gellingsco@p...> on Tue, 04 Feb 2003 20:13:37 +0100
Hi

You can first check the variable with

if(!is_array([var_name])){
	give your error message  ......
}else{
	while(list($key, $value) =each($array_variable)){ ....
	or
	whatever you do
}

m.t.h.

Carl

Message #4 by "David Scott-Bigsby" <DScott-Bigsby@P...> on Tue, 4 Feb 2003 11:49:36 -0800
> The simple thing is:
>
> $result =3D mysql_query($query);
>
> if(!$result)

You can also use the is_resource() function, e.g.,

	if (!is_resource($result)) ...

It adds a bit of processing overhead -- invalid results are literally 
"0" -- but makes the code a bit more readable, IMHO.

> {
>    echo "There was an error running your query.\n"
>       . "Query: $query\n"
>       . "Error: " . mysql_error() . "\n";
> }
> else if(mysql_num_rows($result) =3D=3D 0)
> {
>    echo "No result rows matched.";
> }
> else
> {
>
> }

dsb

***************************************       
David Scott-Bigsby
Product Manager, Web Site and PEDN

PureEdge Solutions
The Leader in Secure XML e-Forms

v:250-708-8145  f:250-708-8010
1-888-517-2675   www.PureEdge.com
***************************************

Message #5 by Kyle Ketterer <BIGE88FAN@c...> on Tue, 04 Feb 2003 15:19:24 -0800
If you want to change the output of the error message of your MySQL
function use, the "or YOUR_FUNCTION" method.

For example, I made my own function for outputting error messages in
MySQL queries. This is a little how it works:

$query = mysql_query("SELECT row FROM table") or myerror("Couldn't
execute this query properly, MySQL returned this
error:<br>".mysql_error()."");

Alternately, use the 'or die()' method, that is if you don't feel like
writing your own function to halt execution of the script and display an
error. 

You could probably even expand on my function above a little more, with
different parameters, but that was just for example.

-Kyle


Message #6 by "Nikolai Devereaux" <yomama@u...> on Tue, 4 Feb 2003 15:39:20 -0800
Hi Kyle,


A couple comments.


> If you want to change the output of the error message of your MySQL
> function use, the "or YOUR_FUNCTION" method.
>
> For example, I made my own function for outputting error messages in
> MySQL queries. This is a little how it works:
>
> $query = mysql_query("SELECT row FROM table") or myerror("Couldn't
> execute this query properly, MySQL returned this
> error:<br>".mysql_error()."");
>
> Alternately, use the 'or die()' method, that is if you don't feel like
> writing your own function to halt execution of the script and display an
> error.
>
> You could probably even expand on my function above a little more, with
> different parameters, but that was just for example.


First, the mysql_query(..) or <function>; convention is pretty widely used,
but it's not by any means the only (nor best) way to do things.


Since any db-driven site can use potentially very many queries, I'd suggest
writing an additional utility function that wraps myerror().


function querror($query)
{
  myerror("Couldn't execute query \"$query\" properly, "
          . "MySQL returned: \"" . mysql_error() . "\"\n";
}

Now you can clean up all your mysql_query calls to look like this:

$result = mysql_query($query) or querror($query);


You don't need to pass the $query to the function, but I like to do so for
debugging purposes.

This is also pretty nice because you can modify a single function to behave
different ways based on external parameters.

Suppose you can log into a site you're developing as an administrator.

function querror($query)
{
   if(is_admin()) // defined in user.inc or something like that
   {
      myerror("Couldn't execute query \"$query\" properly, "
              . "MySQL returned: \"" . mysql_error() . "\"\n";

   }
   else // normal user gets nice output
   {
      myerror("I'm sorry, the database is experiencing problems.  "
              . "The system administrator has been notified.  "
              . "Please try your request again in an hour or so.");

      // notify administrator of query error
      mail(...);
   }
}



hth,

Nik

Message #7 by "Juliet May" <jmay@s...> on Tue, 4 Feb 2003 17:18:30 -0700
Thanks Nik,

Although I didn't ask the question your solution is a lot cleaner and more
functional than the one I am currently using. As always, I appreciate your
feedback to all the questions posted on this forum.

Julie

<snip>
----- Original Message -----
From: "Nikolai Devereaux" <yomama@u...>
To: "beginning php" <beginning_php@p...>
Sent: Tuesday, February 04, 2003 4:39 PM
Subject: [beginning_php] RE: bringing up a friendly error message instead of
the SQL Error


>
> Hi Kyle,
>
>
> A couple comments.
>
</snip>


  Return to Index