catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.83k stars 1.15k forks source link

QUERY + RAW (PDO::quote() expects parameter 1 to be string) #843

Closed gggglglglg closed 5 years ago

gggglglglg commented 5 years ago

Describe the bug I try use $db->query() with Medoo::raw()

Information

Detail Code

Try without RAW

  $data = $db->debug()->query("
              SELECT <id>, <group> FROM `items` WHERE `group` IN (:groups);
          ", [
          ":groups" => implode(",", [1,2,3])
      ]
  )->fetchAll(PDO::FETCH_ASSOC);

SELECT id, group FROM items WHERE group IN ('1,2,3')

All ok, but IN with ' (quotes)

Now try with RAW

  $data = $db->debug()->query("
              SELECT <id>, <group> FROM `items` WHERE `group` IN (:groups);
          ", [
          ":groups" =>  \Medoo\Medoo::raw(implode(",", [1,2,3])) 
      ]
  )->fetchAll(PDO::FETCH_ASSOC);

PDO::quote() expects parameter 1 to be string, object given in

Expected output

Need

SELECT id, group FROM items WHERE group IN (1,2,3) IN without ' (quotes)

catfan commented 5 years ago

It's unable to bind array as placeholder. The PDO didn't support for this.

But for your case, it's completely possible using Medoo select() to handle this.

Medoo's select has a special function to deal with IN (xxx, xxx).