mysql_error (was Re: [thelist] RE: [OT] god why isn't this thread done with)

Matt Warden mwarden at gmail.com
Mon Jan 31 09:31:30 CST 2005


> Why didn't my query work? Why can't I connect to my database? What went
> wrong?
> 
> PHP provides a handy little function for most databases that allow you
> to get a clearer view of what happened, mysql_error(). An example when
> used with a query...
> 
> if(!($result = mysql_query($myQuery, $myDatabaseConnection))){
>    echo mysql_error() . "\n";
>    exit();
> }

You can actually just do this:

$result = mysql_query($sql) or die(mysql_error());

if mysql_query() returns false, then the second part of the or will be
executed. There you have your error message.

Of course, it's your funural if you leave the die() in there
post-launch. A creative use of constants and another OR could get
around this problem, though.


-- 
Matt Warden
Miami University
Oxford, OH, USA
http://mattwarden.com


This email proudly and graciously contributes to entropy.


More information about the thelist mailing list