Robin
Robin

Reputation: 83

PDO bindValue isn't allowing a specific parameter to pass through (PHP)

I am using PHP with PDO for PostgreSQL. I am getting weird behavior when I try to bind a specific value into my query. What I have is the following:

$value[0][$i] = "'%{$filtervalue}%'";
$values[] = & $value[0][$i];
$result = $pdo->prepare($query);
$result->bindValue(':condition', $values[0]);
$result->bindValue(':starts', $start);
$result->bindValue(':pagesize', $pagesize);

Now the $query parameter returns back

SELECT orderDate, shippeddate, shipName, 
shipaddress, shipcity, shipcountry 
FROM orders  WHERE ( shipcity LIKE :condition ) 
LIMIT :pagesize OFFSET :starts 

which is exactly what I want. However the issue is with the $vales[0] parameter because $pagesize and $start work fine.

When I execute with the $vales[0] parameter the results returns back nothing.

However, when I change

$result->bindValue(':condition', $values[0]);

To

$result->bindValue(':condition', '%Bern%');

it works for that particular condition and the results are shown.

I doubled checked to make sure $values[0] returns back '%Bern%' and it does.

I even checked to see the type is string and it is.

I even added PDO::PARAM_STR and still nothing.

I ran out of ideas on why it isn't working.

Upvotes: 3

Views: 34

Answers (1)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

2 changes needed:

$value[0][$i] = "'%{$filtervalue}%'"; needs to be $value[0][$i] = "%{$filtervalue}%";

And

$values[] = & $value[0][$i]; needs to be $values[] = $value[0][$i];

Note:- You can directly use $value[0][$i], no need to create an extra variable, do like this:

$value[0][$i] = "%{$filtervalue}%";
$result = $pdo->prepare($query);
$result->bindValue(':condition', $value[0][$i]);
$result->bindValue(':starts', $start);
$result->bindValue(':pagesize', $pagesize);

Upvotes: 2

Related Questions