我试图从外部数据库获取结果用于wordpress,外部数据库用于phpbb。我有一个我知道正常工作的函数,因为 echo $ themeta [0]; 返回我想要的东西。但是由于某种原因,当我尝试执行var_dump($ forum_results)时,DB select语句不能给我 NULL 。有没有人知道我能做些什么来解决这个问题?
if ( ! function_exists( 'forum_database' ) ) :
/**
* Forum database
*/
function forum_database() {
$dbhost = 'localhost';
$dbname = 'databasename';
$dbuser = 'user2013';
$dbpasswd = 'pass2013';
$forum_table_b = 'phpbb_attachments';
$forum_table_a = 'phpbb_topics';
$forum_db = new wpdb($dbname, $dbuser, $dbpasswd, $dbhost);
$key = 'forum_id';
$themeta = get_post_custom_values($key, $post->ID);
if( ! empty( $themeta ) ) {
echo $themeta[0];
$forum_results = $forum_db->get_results("SELECT topic_title FROM phpbb_topics WHERE forum_id = $themeta[0]");
var_dump($forum_results);
/*foreach ( $forum_results as $forum_result )
{
echo $forum_result->topic_title;
}*/
}
}
endif;
答案 0 :(得分:0)
您可以清楚地看到wpdb class first参数的构造函数是 dbuser 然后是密码,然后是 db name ,最后是主机强>
function __construct( $dbuser, $dbpassword, $dbname, $dbhost ) {
register_shutdown_function( array( $this, '__destruct' ) );
if ( WP_DEBUG )
$this->show_errors();
$this->init_charset();
$this->dbuser = $dbuser;
$this->dbpassword = $dbpassword;
$this->dbname = $dbname;
$this->dbhost = $dbhost;
$this->db_connect();
}
在您的代码中,您以错误的顺序传递参数
$forum_db = new wpdb($dbname, $dbuser, $dbpasswd, $dbhost);
应该是
$forum_db = new wpdb($dbuser, $dbpasswd, $dbname , $dbhost);
要查看错误,您可以使用$forum_db->show_errors();
或$forum_db->print_error();