$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
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.
$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.
Store the error message generated in an sql query or send email >>
dealer pulsa | 28-05-2010 |
thanks for sharing, It's usefull for me |
Sofie | 30-11-2012 |
Failed to read auto-increment value from storage engine |