Reputation: 2178
For many queries, it's faster and easier to just use the question-mark notation for a query. There are various guides and posts about getting the final run query by extending PDO, but I never see one that works when using the question-mark notation. Can this be done? I'm running a query that appears by all accounts to work, but it's not returning results in the actual PDO code for some reason. What does it take to actually get the EXACT final query that PDO is running so I can see where the error is?
$sth = $dbh->prepare("SELECT fromID,toID,m_key FROM messages WHERE (fromID = ? AND toID IN (?)) OR (toID = ? AND fromID IN (?)) AND message LIKE 'addfriend'");
$sth->execute(array($_SESSION['userID'],$include,$_SESSION['userID'],$include));
Upvotes: 1
Views: 103
Reputation: 1203
The below method will help you to substitute the value ($params) against ?
or :param
placeholder. So that we can see the exact query to be executed.
Note: It is useful only for development debugging purpose.
public static function interpolateQuery($query, $params) {
$keys = array();
$values = $params;
# build a regular expression for each parameter
foreach ($params as $key => $value) {
if (is_string($key)) {
$keys[] = '/:'.$key.'/';
} else {
$keys[] = '/[?]/';
}
if (is_array($value))
$values[$key] = implode(',', $value);
if (is_null($value))
$values[$key] = 'NULL';
}
// Walk the array to see if we can add single-quotes to strings
array_walk($values, create_function('&$v, $k', 'if (!is_numeric($v) && $v!="NULL") $v = "\'".$v."\'";'));
$query = preg_replace($keys, $values, $query, 1, $count);
return $query;
}
Upvotes: 1
Reputation: 34285
The problem is not with the use of the ?
placeholder, but the fact that you try to bind a single variable to represent a list of variables in the in
operator. You have to provide as many ?
placeholders in the in
operator separated by commas as the number of parameters you want to have there and you need to bind to each placeholder separately.
...fromID IN (?, ?, ?, ..., ?)...
Upvotes: 2