妹blog 数据转换 Wordpres.php

使用说明:将 imouto_article 和 wp-post 数据表放在同一个数据库,并执行该脚本

 

/**
 * Created by PhpStorm.
 * User: Ylune
 * Date: 2017/10/6
 * Time: 12:09
 */

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(-1);

define('MYSQL_HOST', 'localhost');
if(!defined('MYSQL_DATABASE'))
{
    define('MYSQL_USERNAME', 'root');
    define('MYSQL_DATABASE', 'convert');
    define('MYSQL_PASSWORD', 'root');
}

$fields = [
    'post_author',
    'post_date',
    'post_date_gmt',
    'post_content',
    'post_title',
    'post_name',
    'post_modified',
    'post_modified_gmt',
    'post_type',
];

$DB = @mysqli_connect(MYSQL_HOST, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATABASE);

if (!$DB)
    exit('数据库连接失败');

$DB->set_charset('utf8');

$rs = $DB->query('SELECT * FROM `imouto_article`;');
$articles = $rs->fetch_all(MYSQLI_ASSOC);

for ($i = 0; $i < count($articles); $i++) {

    $field=[];
    $field['title']= $articles[$i]['title'];
    $field['date'] = date('Y-m-d H:i:s', $articles[$i]['created']);
    $field['modified_date'] = date('Y-m-d H:i:s', $articles[$i]['modified']);
    $field['text'] = $articles[$i]['text'];
    $field['post_name'] = urlencode($field['title']);

    $sql = 'INSERT INTO wp_posts ';
    $sql.= '('.implode(',',$fields).')';
    $sql.= ' VALUE(';
    $sql.= "1,";
    $sql.= "'".$field['date']."',";
    $sql.= "'".$field['date']."',";
    $sql.= "'".$field['text']."',";
    $sql.= "'".$field['title']."',";
    $sql.= "'".$field['post_name']."',";
    $sql.= "'".$field['modified_date']."',";
    $sql.= "'".$field['modified_date']."',";
    $sql.= "'post')";

    print_r($sql);

    var_dump($DB->query($sql));
    print mysqli_error($DB);

}