Как использовать PHP-объекты PDO для отправки запросов на обновление SQL - PullRequest
1 голос
/ 16 мая 2019

Я пытаюсь написать скрипт php для выполнения некоторых операций поиска и замены в поле post_content.Однако в моем запросе на обновление что-то не так.

Сначала я попытался выполнить обновление с помощью $db->query(), но оно не сработало.Затем я попытался использовать ::prepare() и ::execute(), но опять-таки у меня ничего не получилось.


// Get all the IDs in a PDO querystring
$ids =  $db->query('SELECT ID FROM XXX_posts WHERE post_content LIKE "%item=\"174\"%" AND post_type="page" AND post_title LIKE "%AZ-%"');
// Iterate through the querystring and create an array
$id_array = array();
foreach( $ids as $id) {
        array_push($id_array, $id[0]);
}

// for each id in the array, store post_content into a variable and run some replacements
// then update post_content with the modified content
foreach ( $id_array as $id ) {
        $query = 'SELECT post_content FROM XXX_posts WHERE ID = ';
        $query .= $id;
        $contents = $db->query($query);

        $final_content = "";
        foreach( $contents as $content ) {
                $final_content = $content[0];
        }

        $output=preg_replace('/961,960,959,913,912,911,910,909,908,907/',
                             '8661,8667,8660,8664,8665,8663,8662,8668,8666,8669',
                             $final_content );
        $output=preg_replace('/4566/', '8678', $output);
        $output=preg_replace('/174/', '8656', $output);
        $output=preg_replace('/174/', '8678', $output);
        $statement = 'UPDATE XXX_posts SET post_content = ' . "'" .  $output . "'"  . ' WHERE XXX_posts.ID = ' . $id . ';';
        $update = $db->prepare($statement);
        $update->execute();
}

Я хотел бы просто выполнить запрос UPDATE для всех 64 идентификаторов сообщений, захваченных на первом шагемоего кода.

Я ценю помощь.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...