Can we use $this->wpdb in class file instead of declare $wpdb in every method
-
I have developed a plugin and testing using (plugin check) . I got so many errors like :
code :WordPress.DB.PreparedSQL.NotPrepared
message : Use placeholders and $wpdb->prepare(); found $query
Here is my code snippet
If i use like $wpdb->prepare(), It does not gives any error, I don’t want to declare global $wpdb in every method that is why i used wpdb in constructor.class Sales_Helper {
private $wpdb;
public $table_prefix;
public function __construct() {
global $wpdb;
$this->wpdb = $wpdb;
$this->table_prefix = $wpdb->prefix;
}
public function scheduled_live_sales_list() {
// global $wpdb; If i use this wpdb inside function, It pass the test
// Sanitize and prepare the table names
$table_sales = esc_sql($this->table_prefix . TABLE_SALES);
$table_overlays = esc_sql($this->table_prefix . TABLE_OVERLAYS);
$status_value = DEFAULT_TRUE;
$query = $this->wpdb->prepare(
"SELECT t1.id, t1.product_id, t1.scheduled_at, t1.timezone, t1.thumb, t1.created_at, t2.overlay
FROM $table_sales t1
LEFT JOIN $table_overlays t2
ON t1.id = t2.id
WHERE t1.status = %d
ORDER BY t1.id DESC",
$status_value
);
// Execute the prepared query
$list = $this->wpdb->get_results($query);
return $list;
}
}What should i do now, Please suggest.
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
- The topic ‘Can we use $this->wpdb in class file instead of declare $wpdb in every method’ is closed to new replies.