0

I have a simple sql select query (similar to below) that returns zero results when using placeholders:

$sql='SELECT * FROM table
        WHERE column1=?
            AND column2=?;';
$sth = $dbh->prepare($sql);
$sth->execute(array(1,2));

However when i hardcode the sql I am receiving results:

$sql='SELECT * FROM table
        WHERE column1=1
            AND column2=2;';
$sth = $dbh->prepare($sql);
$sth->execute();

Can anyone explain why my placeholders aren't working?

EDIT The prepare statement succeeds. Here is the full query:

$sql='SELECT * FROM calendar
LEFT JOIN class_schedule
    ON calendar.week=class_schedule.week 
        AND calendar.class_day=class_schedule.class_day
        AND calendar.class_time=class_schedule.class_time
LEFT JOIN class_enrolledIn
    ON class_enrolledIn.class=class_schedule.class
LEFT JOIN users
    ON class_enrolledIn.u_id=users.id
WHERE calendar.week=?
    AND calendar.class_day=?
    AND u_id IS NOT NULL;';
thegalah
  • 498
  • 1
  • 6
  • 17
  • 2
    Are you certain the `prepare()` succeeds? See [How can I squeeze an error message out of PDO](http://stackoverflow.com/questions/3726505/how-to-squeeze-error-message-out-of-pdo) and make sure you have set PDO up to throw useful errors instead of error silently (its default behavior) Also, what are your real column names? I presume not `column1,column2`. Post your real query, as it might matter... – Michael Berkowski Jan 27 '14 at 03:40
  • I checked the prepare statement and it succeeds through numerous tests. I have also tried surrounding the execute statment if(!$sth->execute(..)) and it also succeeds. – thegalah Jan 27 '14 at 04:55
  • You also need to tell us how it is failing. what result do you get instead of what you expect? There could also be errors in PHP logic. We need to see more code. – Michael Berkowski Jan 27 '14 at 11:46

1 Answers1

0

I've fixed it. But still not 100% sure why an integer does not work as input into an enums field as it works with hard coded sql.

Basically something to do with enums and int

thegalah
  • 498
  • 1
  • 6
  • 17