简体   繁体   中英

PHP PDO Delete from array

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...

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM