Jump to content

[SOLVED] PDO execute() Doesn't recognize my placeholders i.e. ? or :variable


ridiculous

Recommended Posts

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!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.