$PDO_db = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=UTF-8' , DB_USER, DB_PWD);
$sth = $PDO_db->prepare($insert);
$arrPar = array(':nome' => $nome);
$r = $sth->execute($arrPar);
var_dump($arrPar);
print_r($PDO_db->errorInfo());
var_dump($r);
exit();
Supppose that the $insert statemet contains a SQL error, with the code above I've noticed the following :
Code A
print_r($PDO_db->errorInfo());
will output anyways:
Array (
[0] => 00000
[1] =>
[2] =>
)
but var_dump($s) is false
If in the insert statement I have some placemark
e.g. :name
"Insert into mytable (name) VALUE(:name);"
And then in the $arrPar = array(':name' => $value)
with $value=NULL
print_r($PDO_db->errorInfo());
will output again:
Array
(
[0] => 00000
[1] =>
[2] =>
)
but var_dump($s) is false
So I'm wondering to know what's the correct way to debug MySQL statements with PDO if the method errorInfo, which is supposed to give me a error description, silently quiets errors?
PDO::errorInfo()
returns an array of error information"
Strange... are you sure nothing is being iserted? MySQL's return code on success is 0, which the errorInfo
does return. But like @Jack said:
try
{
$PDO_db = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=UTF-8' , DB_USER, DB_PWD);
$PDO_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $PDO_db->prepare($insert);
echo 'Insert prepared<br/>';
$arrPar = array(':nome' => $nome);//shouldn't this be :name?
$r = $sth->execute($arrPar);
echo 'Insert executed<br/>';
var_dump($r);
}
catch(PDOException $pdoE)
{
echo $pdoE->getMessage().'<br/>';
var_dump($pdoE);
}
exit();//?
That should give you more clues as to what is going on. Also, you state (twice) that a var_dump
of $s
is false, but you're assigning the return value of the execute call to $r
... I'm assuming that's a typo, but you never know, hence the comment on ':nome'
.
Also take @Clarence's advice to heart, and set your php.ini error reporting to E_ALL | E_STRICT
, you might hate the vast amount of warnings it issues at first, but it avoids any issues you might (and probably will) encounter when you deploy your code on another machine, or you upgrade your PHP version. Anyway: it's not like PHP is that strict of a language that E_STRICT
is going to cost you huuuge amounts of time to fix the warnings...