Reputation: 1320
I have an array of GUIDs:
Array
(
[0] => 1770302746_580821208_2068022554_1652435499
[1] => 595039408_884272175_1344680100_1997277231
...
)
which I implode to a comma serparated string*:
1770302746_580821208_2068022554_1652435499,595039408_884272175_1344680100_1997277231
that I then pass to a sql DELETE statement:
$q=$conn->prepare(" DELETE FROM su_posts_att_sort WHERE FileGUID IN ({$GUIDs}) ");
$q->execute();
No rows are deleted however. Placing try / catch blocks to return any exception has not thrown any. Can anyone let me know where I'm going wrong?
*Have also tried with each GUID enclosed in double quotes.
EDIT Here is the submitted sql:
DELETE FROM su_posts_att_sort WHERE FileGUID IN ('1770302746_580821208_2068022554_1652435499','595039408_884272175_1344680100_1997277231')
Works fine through the console...
Upvotes: 1
Views: 939
Reputation: 168773
You need to use single quotes, not double quotes, and certainly not unquoted -- they contain underscores, so they are definitely strings, not numerics.
But rather than just adding quotes, it would be better to use the PDO::quote
method, as this will guarantee that they are using the right quoting, no matter what DB you're using, and no matter what weird characters might be lurking in the data.
You can apply PDO::quote
to all the elements in the array in a single line of code, along with the implode()
by using array_map()
. Something like this:
$guids = implode(',', array_map(array($pdo,'quote'), $guidArray));
Upvotes: 2