note 117454 added to pdostatement.execute
| From: | pere dot pasqual at gmail dot com | Date: | Thu, 11 Jun 2015 15:02:17 +0000 |
| Subject: | note 117454 added to pdostatement.execute | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-202843@lists.php.net to get a copy of this message | ||
It's been 7 years since simon dot lehmann at gmx dot comment, but today I found myself having
problems with a prepared statement involving an INSERT, PDO odbc driver for Microsoft Access and PHP
5.4.7. The prepared statement was done using the prepare + execute method, throwing an ugly
"SQLExecDirect[-3500] at ext\\pdo_odbc\\odbc_driver.c:247" error
and a
42000 ("Syntax error or access violation") SQLSTATE.
He suspects what the problem is and points to a possible solution: using bindParam() or bindValue()
and specify a data type.
Well, that seems to be right identifying the source of the problem, but there is a simpler solution
that worked for me, simpler and that allows you to continue using pdo::prepare() with ? as
parameters and pdo::execute():
the only thing you have to do is, if not done before, a cast of the binded parameters to its
specific type (the type that the database is expecting) before putting them in the array you pass to
pdo::execute($array).
The following code fails, throwing the error above:
<?php
$name = "John";
$length = "1";
$price = "1.78";
$SQL = "INSERT INTO table (name, length, price) VALUES (?,?,?)";
$arra = array($name, $length, $price);
$sth = $msq->prepare($SQL);
$sth->execute($arra);
?>
This one works for me like a charm:
<?php
$name = "John";
$length = (int)"1"; // the database is expecting this type
$price = (float)"1.78"; // the database is expecting this type
$SQL = "INSERT INTO table (name, length, price) VALUES (?,?,?)";
$arra = array($name, $length, $price);
$sth = $msq->prepare($SQL);
$sth->execute($arra);
?>
----
Server IP: 72.52.91.14
Probable Submitter: 62.43.206.58
----
Manual Page -- http://php.net/manual/en/pdostatement.execute.php
Edit -- https://master.php.net/note/edit/117454
Del: integrated -- https://master.php.net/note/delete/117454/integrated
Del: useless -- https://master.php.net/note/delete/117454/useless
Del: bad code -- https://master.php.net/note/delete/117454/bad+code
Del: spam -- https://master.php.net/note/delete/117454/spam
Del: non-english -- https://master.php.net/note/delete/117454/non-english
Del: in docs -- https://master.php.net/note/delete/117454/in+docs
Del: other reasons-- https://master.php.net/note/delete/117454
Reject -- https://master.php.net/note/reject/117454
Search -- https://master.php.net/manage/user-notes.php