As of 5.2.6 you still can't use this function's $input_parameters to pass a boolean to PostgreSQL. To do that, you'll have to call bindParam() with explicit types for each parameter in the query.
PDOStatement->execute
(No version information available, might be only in CVS)
PDOStatement->execute — Executes a prepared statement
Description
Execute the prepared statement. If the prepared statement included parameter markers, you must either:
call PDOStatement::bindParam() to bind PHP variables to the parameter markers: bound variables pass their value as input and receive the output value, if any, of their associated parameter markers
or pass an array of input-only parameter values
Parameters
- input_parameters
-
An array of values with as many elements as there are bound parameters in the SQL statement being executed.
You cannot bind multiple values to a single parameter; for example, you cannot bind two values to a single named parameter in an IN() clause.
Return Values
Returns TRUE on success or FALSE on failure.
Examples
Example #1 Execute a prepared statement with bound variables
<?php
/* Execute a prepared statement by binding PHP variables */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour');
$sth->bindParam(':calories', $calories, PDO::PARAM_INT);
$sth->bindParam(':colour', $colour, PDO::PARAM_STR, 12);
$sth->execute();
?>
Example #2 Execute a prepared statement with an array of insert values (named parameters)
<?php
/* Execute a prepared statement by passing an array of insert values */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour');
$sth->execute(array(':calories' => $calories, ':colour' => $colour));
?>
Example #3 Execute a prepared statement with an array of insert values (placeholders)
<?php
/* Execute a prepared statement by passing an array of insert values */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < ? AND colour = ?');
$sth->execute(array($calories, $colour));
?>
Example #4 Execute a prepared statement with question mark placeholders
<?php
/* Execute a prepared statement by binding PHP variables */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < ? AND colour = ?');
$sth->bindParam(1, $calories, PDO::PARAM_INT);
$sth->bindParam(2, $colour, PDO::PARAM_STR, 12);
$sth->execute();
?>
PDOStatement->execute
08-Aug-2008 06:33
09-Mar-2008 06:38
Hopefully this saves time for folks: one should use $count = $stmt->rowCount() after $stmt->execute() in order to really determine if any an operation such as ' update ' or ' replace ' did succeed i.e. changed some data.
Jean-Lou Dupont.
19-Jan-2008 04:33
When passing an array of values to execute when your query contains question marks, note that the array must be keyed numerically from zero. If it is not, run array_values() on it to force the array to be re-keyed.
<?php
$anarray = array(42 => "foo", 101 => "bar");
$statement = $dbo->prepare("SELECT * FROM table WHERE col1 = ? AND col2 = ?");
//This will not work
$statement->execute($anarray);
//Do this to make it work
$statement->execute(array_values($anarray));
?>
30-Aug-2007 01:20
You could also use switch the order of t1 and t2 to get user_id from t1 (tested on postgresql):
SELECT
t2.*,
t1.user_id, t1.user_name
FROM table1 t1
LEFT JOIN table2 t2 ON t2.user_id = t1.user_id
WHERE t1.user_id = 2
08-Aug-2007 11:17
It seems, that the quoting behaviour has changed somehow between versions, as my current project was running fine on one setup, but throwing errors on another (both setups are very similar).
Setup 1: Ubuntu 6.10, PHP 5.1.6, MySQL 5.0.24a
Setup 2: Ubuntu 7.04, PHP 5.2.1, MySQL 5.0.38
The code fragment which caused problems (shortened):
<?php
$stmt = $pdo->prepare("SELECT col1, col2, col3 FROM tablename WHERE col4=? LIMIT ?");
$stmt->execute(array('Foo', 1));
?>
On the first Setup this executes without any problems, on the second setup it generates an Error:
SQLSTATE[42000]: Syntax error or access violation: 1064 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 ''1'' at line 1
The problem is, that $stmt->execute() quotes the number passed to the second placeholder (resulting in: ... LIMIT '1'), which is not allowed in MySQL (tested on both setups).
To prevent this, you have to use bindParam() or bindValue() and specify a data type.
08-Jan-2007 09:16
For a query like this:
SELECT
t1.user_id, t1.user_name,
t2.*
FROM table1 t1
LEFT JOIN table2 t2 ON t2.user_id = t1.user_id
WHERE t1.user_id = 2
If I don't have an entry in table2 for user_id=2, the user_id in result will be empty.
SELECT
t1.user_id, t1.user_name,
t2.user_pet, t2.user_color, t2.user_sign
FROM table1 t1
LEFT JOIN table2 t2 ON t2.user_id = t1.user_id
WHERE t1.user_id = 2
This query will return nonempty user_id.
So please be careful with wildcard select.
23-Dec-2006 06:38
An array of insert values (named parameters) don't need the prefixed colon als key-value to work.
<?php
/* Execute a prepared statement by passing an array of insert values */
$calories = 150;
$colour = 'red';
$sth = $dbh->prepare('SELECT name, colour, calories
FROM fruit
WHERE calories < :calories AND colour = :colour');
// instead of:
// $sth->execute(array(':calories' => $calories, ':colour' => $colour));
// this works fine, too:
$sth->execute(array('calories' => $calories, 'colour' => $colour));
?>
This allows to use "regular" assembled hash-tables (arrays).
That realy does make sense!
21-Nov-2006 06:54
If you don't want to turn on exception raising, then try this:
//$dbErr = $dbHandler->errorInfo(); OR
$dbErr = $dbStatement->errorInfo();
if ( $dbErr[0] != '00000' ) {
print_r($dbHandler->errorInfo());
die( "<div class='redbg xlarge'>FAILED: $msg</div><br />".$foot);
// or handle the error your way...
}
echo "SUCCESS: $msg<br />";
... continue if succesful
24-Oct-2006 09:53
Just a note, I'm currently using 5.1.6. After debugging for a good amount of time, I've realized that the $array cannot be an associative array.
For instance, the following code will fail on the execute.
<?php
$fields = array();
$fields['name'] = "Someone";
$query = $PDO->prepare("insert table set name=?");
$query->execute($fields);
?>
For my code, I had to convert the $fields array into a non-associative array. Something like this:
<?php
$fields = array();
$fields['name'] = "Someone";
$values = array();
foreach($fields as $value) { $values[] = $value; }
$query = $PDO->prepare("insert table set name=?");
$query->execute($values);
?>
15-Oct-2006 10:42
I've used it and it returns booleans=>
$passed = $stmt->execute();
if($passed){
echo "passed";
} else {
echo "failed";
}
If the statement failed it would print failed. You would want to use errorInfo() to get more info, but it does seem to work for me.
