如何使用PHP';s PDO对象发送SQL更新查询
我正在尝试编写一个php脚本来对post_内容字段执行一些搜索和替换操作。但是,我的更新查询有问题 首先,我尝试使用如何使用PHP';s PDO对象发送SQL更新查询,php,mysql,sql,wordpress,Php,Mysql,Sql,Wordpress,我正在尝试编写一个php脚本来对post_内容字段执行一些搜索和替换操作。但是,我的更新查询有问题 首先,我尝试使用$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\"%"
$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();
}
我只想在代码的第一步中捕获的所有64个post ID上运行更新查询
我很感激帮助。在您的第一个查询中,您需要<代码> GROPK-CONTATA IDS,在第二个查询中,您将使用<代码>其中ID在(YousS.IsSuin)中。它很容易使用。[我写过]@danish-khan-I你应该在你的评论中添加你写这个类的事实…@danish-khan-I我不明白你说的使用
group_concat
是什么意思——在foreach
循环中?你实际上不需要foreach
你可以在一个查询中包含所有ID。而不是运行100次。在您的第一个查询中,您需要<代码> GROPK-CONTAT < /COS> IDS,并且在第二个查询中,您将使用<代码>其中ID在(YousS.IsSuin)中。它很容易使用。[我写过]@danish-khan-I你应该在你的评论中添加你写这个类的事实…@danish-khan-I我不明白你说的使用group_concat
是什么意思——在foreach
循环中?你实际上不需要foreach
你可以在一个查询中包含所有ID。而不是运行100次。