-1

I'm fairly new to PDO. I have a try and catch which catches and displays errors when something doesnt exist i.e a table.

However, how can i show the error message/cause for sql failed commands.

For example below i was trying to insert the word "enabled" into a tiny int column - however, only showed me a blank screen - had to debug myself. How can i show SQL failed error messages?

$db = new PDO('mysql:host='.$dateBaseHost.';dbname='.$dateBaseName, $dateBaseUsername, $dateBasePassword);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// set/get variables
$id = (isset($_GET['id']) === true) ? $_GET['id'] : die("ID not set") ; // ? : shorthand if else
$action = (isset($_GET['action']) === true) ? $_GET['action'] : die("Action not set") ; // ? : shorthand  if else


// query
$query = $db->prepare(" UPDATE `telephoneCountries` SET `enabled`= :action  WHERE `id` = :id ");


// bind parameters - avoids SQL injection
$query->bindValue(':id', $id);
$query->bindValue(':action', $action);

// try... if not catch exception
try {
    // run the query
    $query->execute();
}
catch (PDOException $e){
    //sendErrorMail($e->getMessage(), $e->getFile(), $e->getLine());
    echo $e->getMessage();
    echo $e->getFile();
    echo $e->getLine();
}
user2183216
  • 359
  • 3
  • 9
  • 22

1 Answers1

6

i was trying to insert the word "enabled" into a tiny int column

it's fairly OK to mysql. 0 will be inserted.

how can i show the error message/cause for sql failed commands.

for the real errors you have to just setup PHP to display them

ini_set('display_errors',1);

so - you'll be able to see every uncaught exception.

Also, if you are only going to display an error message, but not handle the error itself, just don't use try..catch at all. PHP will do all the job already. That's the point.

Most people do confuse error reporting with error handling. The latter shouldn't be used for the former. When dealing with error messages, your only goal is to make PHP to raise them, and to set up the proper destination:

  • on a development server an error message have to be show on-screen
  • on a live sever it shouldn't be shown, but logged instead.

without all these try-catch blocks you'll be able to control error messages (including non-exceptions) by means of a couple ini settings or single error handler function (which I mentioned to you in the other answer).

use try..catch only if you are going to handle the error itself - say, to connect to another server for example.

So, to answer your question more verbosely:

  1. Set PDO in exception mode. Done already.
  2. Remove all try..catch blocks that deals with error messages only.
  3. Setup PHP to show errors on a development server using ini directive above.
  4. On a live server it is strongly recommended to log errors instead of emailing them. But if you still want it this way - use single custom exception handler function to send emails instead of hundreds try..catch blocks
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345