Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

postgresql

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.

like image 579
Robin Avatar asked Dec 07 '25 03:12

Robin


1 Answers

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);
like image 104
Anant Kumar Singh Avatar answered Dec 08 '25 15:12

Anant Kumar Singh