-
chad.lung =at= gmail.com
Please do not email me seeking help with your project or code
Follow via RSS or ATOM
Search
-
March 2012 M T W T F S S « Feb 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Categories
- ActionScript (33)
- Adobe (18)
- AIR (3)
- AJAX (11)
- Amazon Web Services (3)
- Android (12)
- Apache Abdera (9)
- Apache Mina (2)
- Apache Tomcat (7)
- App Engine (6)
- ASP.NET (9)
- Atom Hopper (9)
- BlazeDS (1)
- C# (14)
- CherryPy (1)
- Comet (3)
- CouchDB (2)
- Design Patterns (3)
- Dojo (1)
- Erlang (1)
- Flash (25)
- Flex (31)
- Go (1)
- GWT (20)
- HTML5 (23)
- iPhone (1)
- Java (63)
- JavaFX (1)
- JavaScript (42)
- JSON Feed Server (1)
- Mobile (17)
- MongoDB (6)
- Netbeans (18)
- Node.js (18)
- Online Games (11)
- Open Source (61)
- Perl (1)
- PHP (23)
- Python (9)
- Rails (3)
- REST (4)
- Reviews (6)
- Ruby (11)
- Silverlight (3)
- Sinatra (3)
- Socket Server (19)
- Ubuntu (33)
- Uncategorized (5)
- Union Framework (8)
- Vaadin (2)
- WebORB (3)
Getting basic error information from PHP PDO
My recent PHP PDO article introduced you to using PHP PDO. One thing I didn’t cover was how to actually get some error information back from PDO if perhaps you had buggy SQL or something.
Let assume I have a simple database called MyDB along with a table called MyTable.
Here is a MySQL script for creating the table:
CREATE TABLE `MyDB`.`MyTable` ( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY , `value1` VARCHAR( 20 ) NOT NULL ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;
Perhaps there is a row in there I want to update so I craft my PHP like so:
<?php error_reporting(E_ALL); ini_set("display_errors", 1); try { $db = new PDO("mysql:host=localhost;dbname=MyDB", 'user', 'password'); $stmt = $db->prepare("UPDATE MyTable SET value1 = :value1 WHERE id = :id)"); $id = 100; $value1 = 'Hello World'; $stmt->bindParam(':value1', $value1); $stmt->bindParam(':id', $id); echo($stmt->execute()); } catch(Exception $e) { echo($e->getMessage()); } ?>
If you have a sharp eye you will have noticed on the end of my SQL statement I have a bracket which will make the SQL fail. Unfortunately though even with the debugging turned on and a try/catch I won’t know the update failed. I even checked for any return value from the execute function.
So, how do I figure out what happened? Well, you can use PDOStatement::errorInfo(). Let me rewrite it now so we get back a message on what went wrong. I’ll add the following line:
print_r($stmt->errorInfo());
Here is the full listing:
<?php error_reporting(E_ALL); ini_set("display_errors", 1); try { $db = new PDO("mysql:host=localhost;dbname=MyDB", 'user', 'password'); $stmt = $db->prepare("UPDATE MyTable SET value1 = :value1 WHERE id = :id)"); $id = 100; $value1 = 'Hello World'; $stmt->bindParam(':value1', $value1); $stmt->bindParam(':id', $id); echo($stmt->execute()); print_r($stmt->errorInfo()); } catch(Exception $e) { echo($e->getMessage()); } ?>
The output is:
Array ( [0] => 42000 [1] => 1064 [2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1 )
Ok, so mixed into that we finally find out what went wrong. If you want just the error message you can just do this:
$arr = $stmt->errorInfo(); echo($arr[2]);
Result:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
There are a few other functions you can call as well to get error codes or error information:
- PDO::errorInfo
- PDO::errorCode
- PDOStatement->errorCode
- PDOStatement->errorInfo
Comments are closed.