dongzheng4556 2014-05-13 22:15
浏览 48
已采纳

来自SQL的wp_insert_post,Wordpress数据库错误

I need to create wordpress posts using wp_insert_post, and fill "post_title" and "post_content" with values queried from an external (not wordpress) database. No matter what I try, I still get the same error.

Wordpress Database Error: database_name.wp_phppc_functions' doesn't exist]
SELECT * FROM `wp_phppc_functions` WHERE `id` = 31

I have tried the following code as a plugin, inserting into functions.php and as a standalone file but I get the same error. For some reason, the site still thinks it is in the other database? Any help you can give me would be much appreciated!

$conzz = mysql_connect("localhost","username","password");


if (!$conzz) {


  die('Could not connect: ' . mysql_error());


}

mysql_select_db("database_name", $conzz);

$resultzz = mysql_query("SELECT T1.ID, REBATE_CODE, LONG_DESC, INCENT_TECH_ID, UPGRADE_TECH FROM T_L_INCENTIVES T1 INNER JOIN T_L_INCENT_TECH T2 ON T1.L_INCENT_TECH = T2.ID INNER JOIN T_LIGHTING_TYPE T3 ON T2.E_LIGHTING_TYPE_ID = T3.ID");

while($row = mysql_fetch_array($resultzz)) {

$new_post = array(
'post_title' => $row['REBATE_CODE'] ,
'post_content' => $row['LONG_DESC'] ,
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'post',
'post_category' => array(10)
);


$conzzz = mysql_connect("localhost","username","password","wordpress_database");

if (!$conzzz) {

die('Could not connect: ' . mysql_error());


}
mysql_select_db("wordpress_database", $conzzz);

wp_insert_post( $new_post ); 
}
?>
  • 写回答

1条回答 默认 最新

  • duanlipeng4136 2014-05-14 00:00
    关注

    The error you are getting is not related to your plugin, it is the PHP Code for Posts plugin that is missing a table - you should disable or delete this plugin. See the code for it with the table name in this file: http://plugins.svn.wordpress.org/php-code-for-posts/tags/1.2.0/PHPPostCode.php

    That said, you do have issues with your plugin as well. You should not use the mysql_* extensions both because they have been deprecated and because they are not the correct way to access the database using WordPress. The proper way to do this would be to use the global variable $wpdb which abstracts a lot of the database work away from your plugin. A better way to implement your code using the recommended WordPress functions...

    global $wpdb;
    
    $sql = <<<SQL
    SELECT T1.ID, REBATE_CODE, LONG_DESC, INCENT_TECH_ID, UPGRADE_TECH 
    FROM T_L_INCENTIVES T1 
    INNER JOIN T_L_INCENT_TECH T2 
        ON T1.L_INCENT_TECH = T2.ID 
    INNER JOIN T_LIGHTING_TYPE T3 
        ON T2.E_LIGHTING_TYPE_ID = T3.ID
    SQL;
    
    $rebates = $wpdb->get_results( $sql );
    foreach ( $rebates as $rebate ){
        $new_post = array(
            'post_title' => $rebate->REBATE_CODE,
            'post_content' => $rebate->LONG_DESC,
            'post_status' => 'publish',
            'post_date' => date('Y-m-d H:i:s'),
            'post_author' => $user_ID,
            'post_type' => 'post',
            'post_category' => array(10)
        );
        if ( 0 == ( $post_id = wp_insert_post( $new_post ) ) ){
            // an error occurred and $post_id == 0
        } else {
            // the new post_id is $post_id
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么