0

I am trying to bind a PHP array as argument for a SQL stmt. I am using auraSQL extended PDO so it looks like this:

$php_array = ['first', 'second', 'third']
$db->fetchColumn("SELECT * FROM my_table WHERE column IN (:php_array), ['php_array' => $php_array])"

Is there a way to do that? I cannot find out how. I tried to append the $php_array as string separated with commas and wrapped in quotes but that doesn't work.

EDIT: Solution was to use AuraSql function quote like so:

 $db->fetchColumn("SELECT * FROM my_table WHERE column IN (".$db->quote($php_array).")"; 
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Possible duplicate of [How to use php array with sql IN operator?](https://stackoverflow.com/questions/9618277/how-to-use-php-array-with-sql-in-operator) – Gholamali Irani Dec 08 '17 at 15:08
  • I am a bit confused here regarding the usage. Is the $db an object of ExtendedPdo ? In that case I believe you need to query and get the prepared statement and then calling the `fetchColumn` ? There is no wrapper for the `fetchColumn` itself, so this is expected to fail. See https://github.com/auraphp/Aura.Sql/blob/3.x/src/AbstractExtendedPdo.php and https://github.com/auraphp/Aura.Sql/blob/3.x/src/ExtendedPdo.php – Hari K T Dec 09 '17 at 04:56

1 Answers1

0

Take a look at the Aura.Sql documentation for quote(), here: https://github.com/auraphp/Aura.Sql/blob/3.x/docs/getting-started.md#array-quoting

The SQL IN clause requires a comma-delimited list of values. The Aura.Sql quote() method appears to generate that for you.

You should consider using perform method.

https://github.com/auraphp/Aura.Sql/blob/3.x/docs/getting-started.md#the-perform-method

Hari K T
  • 4,174
  • 3
  • 32
  • 51
CXJ
  • 4,301
  • 3
  • 32
  • 62