Note that there are some explanatory texts on larger screens.

plurals
  1. POpdo catch and output mysql errors
    primarykey
    data
    text
    <p>Still trying to get my head around pdo.</p> <p>I have an insert statement that is executed with pdo. insert works great however if there is an error I would like it displayed to the user.</p> <p>I have the below try catch block.</p> <pre><code>try{ $insertuser = $db-&gt;prepare('INSERT INTO `she_she`.`Persons` (`idnumber`,`addedby`,`firstname`, `middlename`, `surname`, `fullname`, `gender`, `birthdate`, `homelanguage`, `department`, `employeetype`, `employeestatus`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)'); $insertuser-&gt;execute(array($idnumber,$user,$firstname, $middlename, $surname, $fullname, $gender, $birthdate, $language, $department, $employmenttype, $personstatus)); } catch(PDOException $exception){ return $exception; } </code></pre> <p>If the query fails, or lets say a duplicate IDNumber, I want this displayed to the user.</p> <p>If I simply try echo the variable $exception it does not work.</p> <p>I want to return the mysql error to the user.</p> <p>Any advice appreciated as always.</p> <p>Thanks, Ryan</p> <p><em>UPDATE</em></p> <p>new code as per proposed answers:</p> <pre><code> try{ $insertuser = $db-&gt;prepare('INSERT INTO `she_she`.`Persons` (`idnumber`,`addedby`,`firstname`, `middlename`, `surname`, `fullname`, `gender`, `birthdate`, `homelanguage`, `department`, `employeetype`, `employeestatus`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)'); $insertuser-&gt;execute(array($idnumber,$user,$firstname, $middlename, $surname, $fullname, $gender, $birthdate, $language, $department, $employmenttype, $personstatus)); } catch(PDOException $exception){ return $exception-&gt;getMessage(); } echo "exception: ".$exception; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload