简体   繁体   English

需要更改MySQL数据库中的值,SQL语言遇到问题

[英]Need to change values in MySQL Database, having trouble with SQL language

I'm developing a website and there is a lot of manual work I have to do (clicking pages and setting templates etc) so I thought why not automate it. 我正在开发一个网站,我需要做很多手动工作(单击页面和设置模板等),所以我想为什么不自动化它。 I have logged into PHPMyAdmin and have located the places in my MySQL database that I want to alter. 我已经登录PHPMyAdmin并找到了要更改的MySQL数据库中的位置。

It consists of two parts, being 'wp_postmeta' and 'wp_posts'. 它由两部分组成,分别是“ wp_postmeta”和“ wp_posts”。 I have a text file with all the 'post_id' that I want to access as well as the content that I want to change the page to. 我有一个文本文件,其中包含我要访问的所有“ post_id”以及要将页面更改为的内容。 I can convert this to an arrat if necessary. 如有必要,我可以将其转换为arrat。

SELECT * 
FROM `wp_postmeta` 
WHERE `post_id` =x
AND `meta_key` LIKE '_wp_page_template'
AND `meta_value` LIKE 'template-blank-4.php'

SELECT*
FROM `wp_posts`
WHERE `post_id` =x

What I'm trying to do is do a loop or else a series of if/else statements where can access my post number and set its page content. 我想做的是做一个循环或一系列if / else语句,在其中可以访问我的帖子编号并设置其页面内容。 I would perform the two actions (being setting the content and setting the page template) separately. 我将分别执行两个操作(设置内容和设置页面模板)。 How do I start? 我该如何开始?

Here is an extremely rough eapmle of what I want to achieve; 这是我要实现的目标的粗略总结。

Array =(1,2,3,4...1200)
Array2=("string1", "string2", "string3"..."string1200")
for (array[i], i<1200) {
SELECT*
from 'wp_posts'
where post_id=i
set post_content="array2[i]"
SELECT*
from 'wp_postmeta'
where post_id=i
AND `meta_key` LIKE '_wp_page_template'
SET `meta_value` TO 'template-blank-4.php'
}

Any help with this would be greatly appreicated 任何帮助将不胜感激

Like Bvcm said in the comments you should not do this over phpMyAdmin, but use a PHP script. 就像Bvcm在评论中说的那样,您不应通过phpMyAdmin执行此操作,而应使用PHP脚本。 As it seems you want to do a data import, i think the mysqli extension is the easyiest way for you. 看来您想进行数据导入,我认为mysqli扩展名对您来说是最简单的方法。 There is no need to use WP_Query unsless you want to reuse this import within wordpress, eg a plugin. 无需使用WP_Query,除非您想在wordpress中重用此导入,例如插件。 If it is possible, put your ids and your content into one array: 如果可能,请将您的ID和内容放入一个数组中:

$data = array(1 => "string1", 2 => "string2", 3 => "string3", ... 1200 => "string1200")

If you have done that, your PHP code could look like this: 如果您这样做了,那么您的PHP代码可能如下所示:

$link = mysqli_connect("localhost", "my_user", "my_password", "world");
foreach ($data as $id => $content) {
    $query_posts = "UPDATE wp_post
             SET post_content = '".$content."'
             WHERE post_id = ".$id;
    mysqli_query($link, $query_posts);
    $query_meta = "UPDATE wp_postmeta
             SET meta_value = 'template-blank-4.php'
             WHERE post_id = ".$id."
             AND meta_key = '_wp_page_template'";
    mysqli_query($link, $query_meta);
}
mysqli_close($link);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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