0

Possible Duplicate:
PHP PDO: Can I bind an array to an IN() condition?

I have only made websites with PHP and MySQL for a hobby, my whole life I've always used unprepared statements until I decided to try PDO queries last night. I've successfuly gotten them all to work except when I use IN(). For Example when i do this:

$stmt3 = $dbConnection->prepare("SELECT * FROM member_search WHERE zip IN(:zip_codes_in_distance)");
$stmt3->execute(array(':zip_codes_in_distance' =>   $zip_codes_in_distance ));
foreach ($stmt3 as $user_list) {
   //do cool stuff here
}

This returns this error:

Syntax error or access violation: 1064

After googling it, I've tried with no success a few of the solutions like using query() instead of execute()

This only happens when I use IN()

the $zip_codes_in_distance are zipcodes in this format '07110', '07109', '07050'

What am I doing wrong?

Community
  • 1
  • 1
user1053263
  • 722
  • 2
  • 16
  • 33
  • 1
    See http://stackoverflow.com/questions/920353/php-pdo-can-i-bind-an-array-to-an-in-condition – FoolishSeth Oct 05 '12 at 03:21
  • Hmm, I did not find that link even while typing in the title to my question, thank you, looking at it now. – user1053263 Oct 05 '12 at 03:23
  • 1
    I had faced a similar issue and found the solutions in this answer quite useful. http://stackoverflow.com/a/1586650 – verisimilitude Oct 05 '12 at 03:25
  • So this is an array problem. Ok, going to convert my query now with these solutions and see if I can get it to work, I'll comment back with a result shortly, thank you both for helping me. – user1053263 Oct 05 '12 at 03:27

1 Answers1

0

Couldn't get this to work correctly using PDO, so I used MySQLi instead. Establish Database Connection in a separate file:

$Connect = new mysqli($host, DB_Username, DB_password, DB_Name);

Ran the Query like this:

include 'DbConnectFIle.php';
$zips = "07110, 07109";
$sql = "SELECT * FROM table WHERE zip IN(".$zips.") ";
        $result = $Connect->query($sql);
        while($row = $result->fetch_assoc()){
                     $MemberName = $row['first_name'];
        }
$Connect->close();

echo $MemberName;

Tried for four hours using PDO, MySQLi was the only way I could get it to work. I used this tutorial: http://codular.com/php-mysqli

user1053263
  • 722
  • 2
  • 16
  • 33