views:

148

answers:

2

I am trying to execute the following sql from php using pdo: SELECT * FROM my_table WHERE name=?.

When I do this:

$sql = 'SELECT * FROM my__table WHERE name=?' ;  
$stmt = $dbconn->prepare($sql);  
$stmt->bindValue(1, $_POST['name'], PDO::PARAM_STR);  
$stmt->execute();

I get an empty result set.

When I do this:

$sql = 'SELECT * FROM my__table WHERE name=\''.$_POST['name'].'\'' ;  
$stmt = $dbconn->prepare($sql);  
$stmt->execute();

I get the row that I need.

The column 'name' is a VARCHAR(32). This bug only happens with strings. When the bound parameter is an sql INTEGER everything works like it is supposed to.

I am using sqlite3, php 5.2.6 under Apache on Ubuntu.

A: 

What about this?

$sql = "SELECT * FROM my__table WHERE name='?'" ;  
$stmt = $dbconn->prepare($sql);  
$stmt->bindValue(1, $_POST['name'], PDO::PARAM_STR);  
$stmt->execute();
Petr Peller
I tried it. That does not work.
php guy
A: 

Both of these should work:

Without using binding

$sql = "SELECT * FROM my__table WHERE name = ? " ;
$stmt = $dbconn->prepare($sql);
$stmt->execute(array($_POST['name']));

Using a named parameter

$sql = "SELECT * FROM my__table WHERE name = :name " ;
$stmt = $dbconn->prepare($sql);
$stmt->bindParam(':name', $_POST['name'], PDO::PARAM_STR);
$stmt->execute(array($_POST['name']));

Mike Valstar
That worked. but it is still a workaround :)At this point I've switched to using integer keys (for an unrelated reason.) But I would still like to know why bound parameters break like this.If anyone still searching, I've found a bug that looks like what I've encountered: http://bugs.php.net/bug.php?id=46803
php guy