我正在尝试使用wordpress函数和$wbdb
我在wordpress之外的脚本,但我无法弄清楚如何做到这一点.
我试过了:
require_once('./wp-load.php' ); // this is the correct path is tested.
class cron extends wpdb {
public function results(){
$sql = 'SELECT sub_id,email,cate_id FROM co_subsriber WHERE status = 0 ORDER BY sub_id ASC LIMIT '.$start.',750'; // $start =0
$records = $wpdb->get_results($sql);
}
}
Run Code Online (Sandbox Code Playgroud)
我收到错误
Warning: Missing argument 1 for wpdb::__construct(), called in wp-db.php on line 578
Warning: Missing argument 2 for wpdb::__construct() called in wp-db.php on line 578
Warning: Missing argument 3 for wpdb::__construct() called in wp-db.php on line 578
Warning: Missing argument 4 for wpdb::__construct() called in wp-db.php on line 578
Notice: Undefined variable: dbuser wp-db.php on line 602 and all other pass, hostname...
Run Code Online (Sandbox Code Playgroud)
无法选择数据库....
我需要提一下
require_once('./wp-load.php' );
Run Code Online (Sandbox Code Playgroud)
并使用简单的PHP,没有OOP与类,它工作正常.
那么我应该扩展哪个班级?
问题是您没有使用正确的参数调用wpdb类的构造函数.
你需要做这样的事情:
class cron extends wpdb {
function __construct() {
parent::__construct( /* params here */ )
}
}
Run Code Online (Sandbox Code Playgroud)
但这完全$wpdb
是不可靠的,因为已经在wp-load.php中实现了
这样做:
require_once('./wp-load.php' );
class Cron {
private $wpdb;
function __construct( $wpdb ) {
$this->wpdb = $wpdb;
}
public function results() {
$sql = 'SELECT sub_id,email,cate_id FROM co_subsriber WHERE status = 0 ORDER BY sub_id ASC LIMIT '.$start.',750'; // $start =0
$records = $this->wpdb->get_results($sql);
}
}
Run Code Online (Sandbox Code Playgroud)
现在你实现你的课程:
$cron = new Cron( $wpdb );
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
286 次 |
最近记录: |