Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP PDO. error number '00000' when query is correct [duplicate]

I have the code below:

$sql3 = "update news set date='$time' where id='2'"; $sql3 = $connect->exec($sql3); if(!$sql3) {     print_r($connect->errorInfo());     $error = $connect->errorInfo();     die ("Error: (".$error[0].':'.$error[1].') '.$error[2]); } 

When I run the script, sometimes I get error number '00000'. I mean it goes intro the IF. and it is all random. output (sometimes):

Array ( [0] => 00000 [1] => [2] => ) 

What should I do to fix this problem ?
PS: The script executes correctly every time.

like image 910
Siamak Motlagh Avatar asked Aug 05 '12 04:08

Siamak Motlagh


People also ask

How can I get error in PDO?

You need to set the error mode attribute PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION. And since you expect the exception to be thrown by the prepare() method you should disable the PDO::ATTR_EMULATE_PREPARES* feature. Otherwise the MySQL server doesn't "see" the statement until it's executed.

How check PDO query is successful in PHP?

de.php.net/manual/en/pdostatement.execute.php says: Returns TRUE on success or FALSE on failure. - So bind it to a variable, e.g. $success = $STH->execute($params); and check that variable against true or false .

What is the purpose of PDO :: errorInfo () data?

PDO::errorInfo only retrieves error information for operations performed directly on the database. Use PDOStatement::errorInfo when a PDOStatement instance is created using PDO::prepare or PDO::query. Support for PDO was added in version 2.0 of the Microsoft Drivers for PHP for SQL Server.

What is PDO :: Errmode_exception?

PDO::ERRMODE_EXCEPTION : This value throws exceptions. In exception mode, if there is an error in SQL, PDO will throw exceptions and script will stop running. Value of PDO::ERRMODE_EXCEPTION is 2. The script will stop executing generating the error which throws the exception.


1 Answers

The PDO error code 00000 means that everything works fine. The reason you're hitting the error-checking code is that $sql3 is returning 0 (no rows were effected) and PHP evaluates that to false. Try explicitly checking for a return false;

if($sql3 === false) 
like image 150
SomeKittens Avatar answered Sep 18 '22 05:09

SomeKittens