PHP PDO Parameters from a function returned array
Posted
by
noko
on Stack Overflow
See other posts from Stack Overflow
or by noko
Published on 2012-03-23T05:05:30Z
Indexed on
2012/03/23
5:29 UTC
Read the original article
Hit count: 164
I've got a function written that runs a query based on parameters passed to the function.
I can't seem to figure out why doing the following returns a result:
function test($function_returned_array)
{
$variable = 'Hello World';
$sql = 'SELECT `name`, `pid`
FROM `products`
WHERE `name` IN (?)';
$found = $this->db->get_array($sql, $variable);
}
While this doesn't return any results:
function test2($function_returned_array)
{
$sql = 'SELECT `name`, `pid`
FROM `products`
WHERE `name` IN (?)';
$found = $this->db->get_array($sql, $function_returned_array[0]);
}
$function_returned_array[0] is also equal to 'Hello World'. Shouldn't they both return the same results?
When I echo the values of $variable and $function_returned_array[0], they are both 'Hello World'
Here's the relevant parts of my PDO wrapper:
public function query(&$query, $params)
{
$sth = $this->_db->prepare($query);
if(is_null($params))
{
$sth->execute();
}
else if(is_array($params))
{
$sth->execute($params);
}
else
{
$sth->execute(array($params));
}
$this->_rows = $sth->rowCount();
$this->_counter++;
return $sth;
}
public function get_array(&$query, $params, $style = PDO::FETCH_ASSOC)
{
$q = $this->query($query, $params);
return $q->fetchAll($style);
}
I'm using PHP 5.3.5.
Any help would be appreciated.
© Stack Overflow or respective owner