ridiculous Posted August 13, 2008 Share Posted August 13, 2008 I'm working on building prepared statements with the PHP PDO database class. http://www.php.net/manual/en/pdostatement.execute.php ============================ I'm using code snippets verbatim off of the PHP documentation page for execute(). I don't seem to have any problems at all with the code without placeholders. i.e. <?php /* Execute a prepared statement by binding PHP variables */ $calories = 150; $colour = 'red'; $sth = $dbh->prepare('SELECT name, colour, calories FROM fruit WHERE calories < $calories AND colour = $colour'); // $sth->execute(); $result = $sth->fetchAll(); print_r ($result); ?> When I print_r using the code above, the resulting array comes out fine. But as soon as I use the placeholder feature, I just get a blank array. I've run the code below on separate builds of PHP 5.2.2 and PHP 5.2.5 on different servers. <?php /* Execute a prepared statement by binding PHP variables */ $calories = 150; $colour = 'red'; $sth = $dbh->prepare('SELECT name, colour, calories FROM fruit WHERE calories < :calories AND colour = :colour'); $sth->bindParam(':calories', $calories, PDO::PARAM_INT); $sth->bindParam(':colour', $colour, PDO::PARAM_STR, 12); $sth->execute(); $result = $sth->fetchAll(); print_r ($result); ?> My code matches the PHP documentation perfectly, but I can't get the execute function to work properly. It's like the parameters aren't binding for some reason. Anyone have any ideas? I've run this code on WAMP and my GoDaddy hosting account with the same result each time. Thanks in advance!! Link to comment https://forums.phpfreaks.com/topic/119476-solved-pdo-execute-doesnt-recognize-my-placeholders-ie-or-variable/ Share on other sites More sharing options...
ridiculous Posted August 13, 2008 Author Share Posted August 13, 2008 You can't use a parameter for the WHERE clause. That's it. Probably because it doesn't have any optimization value when you do so. Link to comment https://forums.phpfreaks.com/topic/119476-solved-pdo-execute-doesnt-recognize-my-placeholders-ie-or-variable/#findComment-615500 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.