SQL PHP HTML ASP JavaScript articles and free scripts to download
SQL PHP MySQL Functions

Error message displaying after execution of a query

We can display error message in case of an error generated by mysql query. This meaning full error message gives idea one the problem or bugs in the script. We can print the error message by using mysql function mysql_error(). This function returns the error message associated with most recently executed query. So it is a good idea to check for error before going to next query. We can even add the error number returned by mysql to this error message. This way we can generate a meaningful detail on the bugs on the script.

$querry = mysql_query("SELECT new_field FROM student");
echo "Error message = ".mysql_error();

In our student table there is no field as new_field. Here here is the message we will get

Error message = Unknown column 'new_field' in 'field list'

Note that mysql_error() is deprecated as of PHP 5.5.0 so better to avoid using this function. So if you are using PDO then we can use errorInfo() to display the returned error message from MySQL

Here is a sample code in PHP using PDO to display record and on failure to display error message.

require 'config-pdo.php'; // database connection string
$pdo=$dbo->prepare('Select no_name from student');
if($pdo->execute()){
echo 'Success<br>';
$row = $pdo->fetch(PDO::FETCH_OBJ);
echo "Name : $row->name ";
}else{
print_r($pdo->errorInfo());
}

In the above code there is an error in sql , there is no column by name no_name. The output will be

Array ( [0] => 42S22 [1] => 1054 [2] => Unknown column 'no_name' in 'field list' )

So to get correct result change the sql part like this .

$pdo=$dbo->prepare('Select * from student');

With this you will get desired output.

Handling PDO errors

If such error occurs what is to be done ? We have three options to handle in case of errors.

We can stop the execution of the script. ( Fatal error : stop execution of code )
We can display warning message ( Warning only, display message and no stoppage of execution )
Remain silent ( continue to execute and display error message if required )

Setting the PDO error handling attribute.

We can use setAttribute to tell how to handle the PDO errors.
Here is a sample

$dbo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); // gives warning only

Here is the complete code.

require 'config-pdo.php'; // database connection string
//$dbo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // generates fatal error
//$dbo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); // gives warning only
$dbo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT); // remain silent

$pdo=$dbo->prepare('Select no_name from student');
if($pdo->execute()){
echo 'Success<br>';
$row = $pdo->fetch(PDO::FETCH_OBJ);
echo "Name : $row->name ";
}else{
print_r($pdo->errorInfo());
}

Now we have given all the three options but commented two and set the attribute to Silent. You can change the commented status and see how the script is behaving in different setattribute values.

It is clear that the above code will generate error message. We can store the error message in a database or we can post ( by mail ) to the programmer about these details.

Store the error message generated in an sql query or send email >>

Number of User Comments : 2


Google+

More on PHP MySQL functions
dealer pulsa28-05-2010
thanks for sharing, It's usefull for me
Sofie30-11-2012
Failed to read auto-increment value from storage engine
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked




HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer