PDO error code always 00000 even when there is an errorHow to get the max of two values in MySQL?SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax — PHP — PDOHow to alter a column and change the default value?Cast from VARCHAR to INT - MySQLReference - What does this error mean in PHP?Adding multiple columns AFTER a specific column in MySQLPDO showing errorPDO UPDATE: Syntax error or access violationPDO Exception ErrorExecuting a bad PDO query does not yield an error

How to understand payment due date for credit card?

How can I improve my formal definitions

I was given someone else's visa, stamped in my passport

Could a complex system of reaction wheels be used to propel a spacecraft?

What is the practical impact of using System.Random which is not cryptographically random?

math mode in ticks ( tikzpicture )

Why haven't the British protested Brexit as ardently like Hong Kongers protest?

What was Captain Marvel supposed to do once she reached her destination?

Create a list of snaking numbers under 50,000

Unity Case sensitive file system on macOS

“all of who” or “all of whom”?

Was it illegal to blaspheme God in Antioch in 360.-410.?

Can two aircraft be allowed to stay on the same runway at the same time?

Eliminate key lookup in execution plan

Ask one verbal question to figure out who is blind and who is mute among three persons

'Horseshoes' for Deer?

Magnetic thread storage?

How do I portray irrational anger in first person?

Find the logic in first 2 statements to give the answer for the third statement

Moscow SVO airport, how to avoid scam taxis without pre-booking?

Is "prohibition against," a double negative?

Lob Logical Read and lob read-ahead reads in NCCI

How can I portray a character with no fear of death, without them sounding utterly bored?

German equivalent to "going down the rabbit hole"



PDO error code always 00000 even when there is an error


How to get the max of two values in MySQL?SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax — PHP — PDOHow to alter a column and change the default value?Cast from VARCHAR to INT - MySQLReference - What does this error mean in PHP?Adding multiple columns AFTER a specific column in MySQLPDO showing errorPDO UPDATE: Syntax error or access violationPDO Exception ErrorExecuting a bad PDO query does not yield an error






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3















I m running PHP 7.2.16



Not sure when started, PDO errorCode() or errorInfo()[0] now always shows 00000 even there is an error



$pdo = new PDO('mysql:host=localhost;dbname=mydb', 'root', 'pwd');
$sth = $pdo->prepare('select now() and this is a bad SQL where a - b from c');
$sth->execute();
$row = $sth->fetchAll();
$err = $sth->errorInfo();
echo $sth->errorCode();
print_r($row);
print_r($err);


And here is the result:



00000Array
(
)
Array
(
[0] => 00000
[1] => 1064
[2] => You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'a bad SQL where a - b from c' at line 1
)


However, I just did a new test, by delete $sth->fetchAll() or get error before this line, it shows correctly:



Array
(
[0] => 42000
[1] => 1064
[2] => You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'a bad SQL where a - b from c' at line 1
)


OK - the solution is that:





get the error code immediately after execute() and before any fetch












share|improve this question


























  • What is 'one' / 'other'? I only see one code. Have you checked $pdo for an error?

    – danblack
    Mar 27 at 23:15

















3















I m running PHP 7.2.16



Not sure when started, PDO errorCode() or errorInfo()[0] now always shows 00000 even there is an error



$pdo = new PDO('mysql:host=localhost;dbname=mydb', 'root', 'pwd');
$sth = $pdo->prepare('select now() and this is a bad SQL where a - b from c');
$sth->execute();
$row = $sth->fetchAll();
$err = $sth->errorInfo();
echo $sth->errorCode();
print_r($row);
print_r($err);


And here is the result:



00000Array
(
)
Array
(
[0] => 00000
[1] => 1064
[2] => You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'a bad SQL where a - b from c' at line 1
)


However, I just did a new test, by delete $sth->fetchAll() or get error before this line, it shows correctly:



Array
(
[0] => 42000
[1] => 1064
[2] => You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'a bad SQL where a - b from c' at line 1
)


OK - the solution is that:





get the error code immediately after execute() and before any fetch












share|improve this question


























  • What is 'one' / 'other'? I only see one code. Have you checked $pdo for an error?

    – danblack
    Mar 27 at 23:15













3












3








3








I m running PHP 7.2.16



Not sure when started, PDO errorCode() or errorInfo()[0] now always shows 00000 even there is an error



$pdo = new PDO('mysql:host=localhost;dbname=mydb', 'root', 'pwd');
$sth = $pdo->prepare('select now() and this is a bad SQL where a - b from c');
$sth->execute();
$row = $sth->fetchAll();
$err = $sth->errorInfo();
echo $sth->errorCode();
print_r($row);
print_r($err);


And here is the result:



00000Array
(
)
Array
(
[0] => 00000
[1] => 1064
[2] => You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'a bad SQL where a - b from c' at line 1
)


However, I just did a new test, by delete $sth->fetchAll() or get error before this line, it shows correctly:



Array
(
[0] => 42000
[1] => 1064
[2] => You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'a bad SQL where a - b from c' at line 1
)


OK - the solution is that:





get the error code immediately after execute() and before any fetch












share|improve this question
















I m running PHP 7.2.16



Not sure when started, PDO errorCode() or errorInfo()[0] now always shows 00000 even there is an error



$pdo = new PDO('mysql:host=localhost;dbname=mydb', 'root', 'pwd');
$sth = $pdo->prepare('select now() and this is a bad SQL where a - b from c');
$sth->execute();
$row = $sth->fetchAll();
$err = $sth->errorInfo();
echo $sth->errorCode();
print_r($row);
print_r($err);


And here is the result:



00000Array
(
)
Array
(
[0] => 00000
[1] => 1064
[2] => You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'a bad SQL where a - b from c' at line 1
)


However, I just did a new test, by delete $sth->fetchAll() or get error before this line, it shows correctly:



Array
(
[0] => 42000
[1] => 1064
[2] => You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'a bad SQL where a - b from c' at line 1
)


OK - the solution is that:





get the error code immediately after execute() and before any fetch









php mysql pdo






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 23:44







SIDU

















asked Mar 27 at 23:04









SIDUSIDU

1,9441 gold badge8 silver badges18 bronze badges




1,9441 gold badge8 silver badges18 bronze badges















  • What is 'one' / 'other'? I only see one code. Have you checked $pdo for an error?

    – danblack
    Mar 27 at 23:15

















  • What is 'one' / 'other'? I only see one code. Have you checked $pdo for an error?

    – danblack
    Mar 27 at 23:15
















What is 'one' / 'other'? I only see one code. Have you checked $pdo for an error?

– danblack
Mar 27 at 23:15





What is 'one' / 'other'? I only see one code. Have you checked $pdo for an error?

– danblack
Mar 27 at 23:15












1 Answer
1






active

oldest

votes


















2















I tested this code with PHP 7.1.23:



$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$sth = $pdo->prepare('select now() and this is a bad SQL where a - b from c');
if ($sth === false)
echo "error on prepare()n";
print_r($pdo->errorInfo());

if ($sth->execute() === false)
echo "error on execute()n";
print_r($sth->errorInfo());



Output:



error on execute()
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 'a bad SQL where a - b from c' at line 1
)


Then I tested the same code, except after disabling emulated prepare:



$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);


Output:



error on prepare()
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 'a bad SQL where a - b from c' at line 1
)

Fatal error: Uncaught Error: Call to a member function execute() on boolean


Moral of the story:



  • When using emulated prepared statements, prepare() is a no-op, and the error is delayed until execute(). I recommend disabling emulated prepare, unless you use a database that doesn't support prepared statements (I don't know of any current version of any RDBMS product that can't do real prepared statements).


  • When checking for an error on prepare(), use $pdo->errorInfo().


  • When checking for an error on execute(), use $stmt->errorInfo().






share|improve this answer

























  • Thanks. You got error because you run the error check before fetchAll etc :) Could you re-test with fetchAll before errorInfo() ?

    – SIDU
    Mar 27 at 23:39







  • 1





    It makes no sense to fetchAll() from a query that has failed. You should check for errors immediately after prepare() or execute(). If you disable emulated prepare, you can't even attempt the fetchAll(), because $sth will be false and you can't call a method from that.

    – Bill Karwin
    Mar 27 at 23:45











  • Yeah. I double check SIDU, which is checking Error before fetch. Thanks

    – SIDU
    Mar 27 at 23:47










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387802%2fpdo-error-code-always-00000-even-when-there-is-an-error%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2















I tested this code with PHP 7.1.23:



$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$sth = $pdo->prepare('select now() and this is a bad SQL where a - b from c');
if ($sth === false)
echo "error on prepare()n";
print_r($pdo->errorInfo());

if ($sth->execute() === false)
echo "error on execute()n";
print_r($sth->errorInfo());



Output:



error on execute()
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 'a bad SQL where a - b from c' at line 1
)


Then I tested the same code, except after disabling emulated prepare:



$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);


Output:



error on prepare()
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 'a bad SQL where a - b from c' at line 1
)

Fatal error: Uncaught Error: Call to a member function execute() on boolean


Moral of the story:



  • When using emulated prepared statements, prepare() is a no-op, and the error is delayed until execute(). I recommend disabling emulated prepare, unless you use a database that doesn't support prepared statements (I don't know of any current version of any RDBMS product that can't do real prepared statements).


  • When checking for an error on prepare(), use $pdo->errorInfo().


  • When checking for an error on execute(), use $stmt->errorInfo().






share|improve this answer

























  • Thanks. You got error because you run the error check before fetchAll etc :) Could you re-test with fetchAll before errorInfo() ?

    – SIDU
    Mar 27 at 23:39







  • 1





    It makes no sense to fetchAll() from a query that has failed. You should check for errors immediately after prepare() or execute(). If you disable emulated prepare, you can't even attempt the fetchAll(), because $sth will be false and you can't call a method from that.

    – Bill Karwin
    Mar 27 at 23:45











  • Yeah. I double check SIDU, which is checking Error before fetch. Thanks

    – SIDU
    Mar 27 at 23:47















2















I tested this code with PHP 7.1.23:



$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$sth = $pdo->prepare('select now() and this is a bad SQL where a - b from c');
if ($sth === false)
echo "error on prepare()n";
print_r($pdo->errorInfo());

if ($sth->execute() === false)
echo "error on execute()n";
print_r($sth->errorInfo());



Output:



error on execute()
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 'a bad SQL where a - b from c' at line 1
)


Then I tested the same code, except after disabling emulated prepare:



$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);


Output:



error on prepare()
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 'a bad SQL where a - b from c' at line 1
)

Fatal error: Uncaught Error: Call to a member function execute() on boolean


Moral of the story:



  • When using emulated prepared statements, prepare() is a no-op, and the error is delayed until execute(). I recommend disabling emulated prepare, unless you use a database that doesn't support prepared statements (I don't know of any current version of any RDBMS product that can't do real prepared statements).


  • When checking for an error on prepare(), use $pdo->errorInfo().


  • When checking for an error on execute(), use $stmt->errorInfo().






share|improve this answer

























  • Thanks. You got error because you run the error check before fetchAll etc :) Could you re-test with fetchAll before errorInfo() ?

    – SIDU
    Mar 27 at 23:39







  • 1





    It makes no sense to fetchAll() from a query that has failed. You should check for errors immediately after prepare() or execute(). If you disable emulated prepare, you can't even attempt the fetchAll(), because $sth will be false and you can't call a method from that.

    – Bill Karwin
    Mar 27 at 23:45











  • Yeah. I double check SIDU, which is checking Error before fetch. Thanks

    – SIDU
    Mar 27 at 23:47













2














2










2









I tested this code with PHP 7.1.23:



$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$sth = $pdo->prepare('select now() and this is a bad SQL where a - b from c');
if ($sth === false)
echo "error on prepare()n";
print_r($pdo->errorInfo());

if ($sth->execute() === false)
echo "error on execute()n";
print_r($sth->errorInfo());



Output:



error on execute()
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 'a bad SQL where a - b from c' at line 1
)


Then I tested the same code, except after disabling emulated prepare:



$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);


Output:



error on prepare()
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 'a bad SQL where a - b from c' at line 1
)

Fatal error: Uncaught Error: Call to a member function execute() on boolean


Moral of the story:



  • When using emulated prepared statements, prepare() is a no-op, and the error is delayed until execute(). I recommend disabling emulated prepare, unless you use a database that doesn't support prepared statements (I don't know of any current version of any RDBMS product that can't do real prepared statements).


  • When checking for an error on prepare(), use $pdo->errorInfo().


  • When checking for an error on execute(), use $stmt->errorInfo().






share|improve this answer













I tested this code with PHP 7.1.23:



$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$sth = $pdo->prepare('select now() and this is a bad SQL where a - b from c');
if ($sth === false)
echo "error on prepare()n";
print_r($pdo->errorInfo());

if ($sth->execute() === false)
echo "error on execute()n";
print_r($sth->errorInfo());



Output:



error on execute()
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 'a bad SQL where a - b from c' at line 1
)


Then I tested the same code, except after disabling emulated prepare:



$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);


Output:



error on prepare()
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 'a bad SQL where a - b from c' at line 1
)

Fatal error: Uncaught Error: Call to a member function execute() on boolean


Moral of the story:



  • When using emulated prepared statements, prepare() is a no-op, and the error is delayed until execute(). I recommend disabling emulated prepare, unless you use a database that doesn't support prepared statements (I don't know of any current version of any RDBMS product that can't do real prepared statements).


  • When checking for an error on prepare(), use $pdo->errorInfo().


  • When checking for an error on execute(), use $stmt->errorInfo().







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 27 at 23:23









Bill KarwinBill Karwin

399k67 gold badges545 silver badges697 bronze badges




399k67 gold badges545 silver badges697 bronze badges















  • Thanks. You got error because you run the error check before fetchAll etc :) Could you re-test with fetchAll before errorInfo() ?

    – SIDU
    Mar 27 at 23:39







  • 1





    It makes no sense to fetchAll() from a query that has failed. You should check for errors immediately after prepare() or execute(). If you disable emulated prepare, you can't even attempt the fetchAll(), because $sth will be false and you can't call a method from that.

    – Bill Karwin
    Mar 27 at 23:45











  • Yeah. I double check SIDU, which is checking Error before fetch. Thanks

    – SIDU
    Mar 27 at 23:47

















  • Thanks. You got error because you run the error check before fetchAll etc :) Could you re-test with fetchAll before errorInfo() ?

    – SIDU
    Mar 27 at 23:39







  • 1





    It makes no sense to fetchAll() from a query that has failed. You should check for errors immediately after prepare() or execute(). If you disable emulated prepare, you can't even attempt the fetchAll(), because $sth will be false and you can't call a method from that.

    – Bill Karwin
    Mar 27 at 23:45











  • Yeah. I double check SIDU, which is checking Error before fetch. Thanks

    – SIDU
    Mar 27 at 23:47
















Thanks. You got error because you run the error check before fetchAll etc :) Could you re-test with fetchAll before errorInfo() ?

– SIDU
Mar 27 at 23:39






Thanks. You got error because you run the error check before fetchAll etc :) Could you re-test with fetchAll before errorInfo() ?

– SIDU
Mar 27 at 23:39





1




1





It makes no sense to fetchAll() from a query that has failed. You should check for errors immediately after prepare() or execute(). If you disable emulated prepare, you can't even attempt the fetchAll(), because $sth will be false and you can't call a method from that.

– Bill Karwin
Mar 27 at 23:45





It makes no sense to fetchAll() from a query that has failed. You should check for errors immediately after prepare() or execute(). If you disable emulated prepare, you can't even attempt the fetchAll(), because $sth will be false and you can't call a method from that.

– Bill Karwin
Mar 27 at 23:45













Yeah. I double check SIDU, which is checking Error before fetch. Thanks

– SIDU
Mar 27 at 23:47





Yeah. I double check SIDU, which is checking Error before fetch. Thanks

– SIDU
Mar 27 at 23:47








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387802%2fpdo-error-code-always-00000-even-when-there-is-an-error%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript