Array elements to string

I have an Array

Array ( [0] => Windows 10 )

and SQL query is:

$sql="SELECT * FROM product";
$sql.=" WHERE os IN (".implode(',',$os).")";

It will print sql query like

SELECT * FROM product WHERE os IN (Windows 10)

But I want something like this:
SELECT * FROM product WHERE os IN (“Windows 10”)

So, add quotes around it.

'"' . implode('","', $os) . '"'

Ok tried like this:

$sql.=" WHERE os IN (" .'"'. implode('","', $os) . '"'.")";

and it worked:

SELECT * FROM product WHERE os IN ("DOS")

With the Doctrine 2 Database Access Layer you can do:

$oses = ['Windows 10','DOS'];
$sql  = 'SELECT * FROM product WHERE os IN (?)';
$stmt = $conn->executeQuery($sql,[$oses],[Connection::PARAM_STR_ARRAY]);
$rows = $stmt->fetchAll();

And be protected from sql injections as well.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.