neha_k
September 7, 2016, 5:29pm
1
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) . '"'
neha_k
September 7, 2016, 5:40pm
3
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.
system
Closed
December 8, 2016, 1:41am
5
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.