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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.