Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO bindParam behaving strangely

Tags:

php

pdo

I am having a problem getting PDO bindParam to work correctly. Using this code:

$foo = "bar";

$stmt_1 = $db->prepare("SELECT * FROM table WHERE foo = $foo");
$stmt_1->execute();
$results_1 = $stmt_1->fetchAll();

$stmt_2 = $db->prepare("SELECT * FROM table WHERE foo = ?");
$stmt_2->bindParam(1, $foo, PDO::PARAM_STR);
$stmt_2->execute();
$results_2 = $stmt_2->fetchAll();

$stmt_3 = $db->prepare("SELECT * FROM table WHERE foo = :foo");
$stmt_3->bindParam(":foo", $foo, PDO::PARAM_STR);
$stmt_3->execute();
$results_3 = $stmt_3->fetchAll();

$results_1 contains only the row(s) where foo = bar, $results_2 is empty, and $results_3 contains every entry in "table"

bindValue has the exact same problem, too. Anyone know what's going on here or what I'm doing wrong?

like image 536
asgallant Avatar asked Feb 28 '26 14:02

asgallant


1 Answers

I solved my problem. For some reason, the variables I was setting from $_POST weren't available to the internal functions of a class I had defined, so I added a parameter array to the class to hold the bound variables, and now everything works.

like image 68
asgallant Avatar answered Mar 02 '26 04:03

asgallant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!