[PDO] Detecting SQL errors?

This is a discussion on [PDO] Detecting SQL errors? within the PHP Language forums, part of the PHP Programming Forums category; Hello One of my PHP5 scripts wouldn't run right because I didn't use the right number of parameters ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-05-2008
Gilles Ganault
 
Posts: n/a
Default [PDO] Detecting SQL errors?

Hello

One of my PHP5 scripts wouldn't run right because I didn't use the
right number of parameters in the SQL command, but PHP didn't return
any error, even after adding a try/catch section:

======
if(!$dbh->query($sql)->fetchColumn()) {
try {
//Expects two parameters -> won't run
$sql = "INSERT INTO mytable VALUES (NULL)";

$dbh->exec($sql);
} catch (Exception $e) {
echo 'Exception raised : ', $e->getMessage(), "\n";
}
}
======

Is there a way to check that a SQL command completed correctly?

Thank you.
Reply With Quote
  #2 (permalink)  
Old 05-05-2008
macca
 
Posts: n/a
Default Re: Detecting SQL errors?

Try

$dbh->exec($sql) or die(print_r($dbh->errorInfo()));
Reply With Quote
  #3 (permalink)  
Old 05-05-2008
Iván Sánchez Ortega
 
Posts: n/a
Default Re: [PDO] Detecting SQL errors?

Gilles Ganault wrote:

[...]
> Is there a way to check that a SQL command completed correctly?


Check out those:
http://php.net/manual/en/pdo.errorcode.php
http://php.net/manual/en/pdo.errorinfo.php


Cheers,
--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Un ordenador no es un televisor ni un microondas, es una herramienta
compleja.
Reply With Quote
  #4 (permalink)  
Old 05-06-2008
Gilles Ganault
 
Posts: n/a
Default Re: [PDO] Detecting SQL errors?

On Tue, 06 May 2008 00:17:14 +0200, Iván Sánchez Ortega
<ivansanchez-alg@rroba-escomposlinux.-.punto.-.org> wrote:
>Check out those:
>http://php.net/manual/en/pdo.errorcode.php
>http://php.net/manual/en/pdo.errorinfo.php


Thanks guys. Problem solved.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 05:50 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0