[SOLVED] Capture the database error

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

[SOLVED] Capture the database error

Post by ljCharlie »

Here's the line.

Code: Select all

$rsInsertCate = mysql_query($query_rsInsert, $myConnection) or die(mysql_error());
Now, when this line generate an error and die, is there a way to capture the error and assign that to a variable? The reason I want to do this is, if a user is uploading or query something from the database and somehow end up with an error, I want to email that error to me so I can verify why the user receive an error from the database.

ljCharlie
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Code: Select all

//using @ to hide the error from the user
$rsInsertCate = @mysql_query($query_rsInsert, $myConnection);
if($rsInsertCate == false){
  //send your email here
  //just add mysql_error() into the message
  $message .= 'The error was: '.mysql_error();
  ....
  .....
   die('Nice error message for the user here.');
}
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post by ljCharlie »

Thank you very much! That works!

ljCharlie
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post by ljCharlie »

One quick question. What is the difference between a die and an exit? The reason is, when the code is die, every code in the page stop executing.

ljCharlie
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

[php_man]exit[/php_man]
[php_man]die[/php_man]
ljCharlie
Forum Contributor
Posts: 289
Joined: Wed May 19, 2004 8:23 am

Post by ljCharlie »

My question is, what is the difference between die and exit? Are they the same thing?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

just follow the links, and read:

die
die -- Equivalent to exit()
Description

This language construct is equivalent to exit().
[/b]
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post by xisle »

die() is an alias of exit(), so they are the same
Post Reply