php - PDO bindParam behaving strangely -
i having problem getting pdo bindparam work correctly. using code:
$foo = "bar"; $stmt_1 = $db->prepare("select * table foo = $foo"); $stmt_1->execute(); $results_1 = $stmt_1->fetchall(); $stmt_2 = $db->prepare("select * table foo = ?"); $stmt_2->bindparam(1, $foo, pdo::param_str); $stmt_2->execute(); $results_2 = $stmt_2->fetchall(); $stmt_3 = $db->prepare("select * table foo = :foo"); $stmt_3->bindparam(":foo", $foo, pdo::param_str); $stmt_3->execute(); $results_3 = $stmt_3->fetchall();
$results_1 contains row(s) foo = bar, $results_2 empty, , $results_3 contains every entry in "table"
bindvalue has exact same problem, too. know what's going on here or i'm doing wrong?
(i'll new answer because misunderstood question in other one.)
by default pdo silently ignores errors , returns empty results. explain #2. example, if table called "table" , didn't quote backticks (table
reserved keyword). turn on error reporting see if case (see below).
in #3, rows might returned because condition tautological: foo = foo
or :foo = :foo
or 'foo' = :foo
.
here's complete program works correctly me. if doesn't work you, it's bug in particular version of php, pdo or mysql.
<?php $db = new pdo('mysql:host=localhost;dbname=test', 'user', 'password'); $db->setattribute(pdo::attr_errmode, pdo::errmode_exception); $foo = 'bar'; $stmt_1 = $db->prepare("select * `table` foo = '$foo'"); $stmt_1->execute(); $results_1 = $stmt_1->fetchall(); $stmt_2 = $db->prepare("select * `table` foo = ?"); $stmt_2->bindparam(1, $foo, pdo::param_str); $stmt_2->execute(); $results_2 = $stmt_2->fetchall(); $stmt_3 = $db->prepare("select * `table` foo = :foo"); $stmt_3->bindparam("foo", $foo, pdo::param_str); $stmt_3->execute(); $results_3 = $stmt_3->fetchall(); print_r($results_1); print_r($results_2); print_r($results_3);
Comments
Post a Comment