F
F
Fomax_Web2019-08-01 22:02:14
PHP
Fomax_Web, 2019-08-01 22:02:14

How to write data from wpdb->get_results() Wordpress to php class property?

Let's say we have a "User" class, a private id property, and a private property that should contain the data retrieved with wpdb->get_results(). I can't figure out how to implement this request inside the class. Basically, I want this:

class User{

  private $id;
  private $data_from_wbdb_get_results;

  publick function __construct($id){
    $this->id = $id;
    $this->data_from_wbdb_get_results = $wpdb->get_results("SELECT * FROM table WERE id=$this->id");
  }

}

It certainly doesn't work, I don't know how to do it right. How to declare global $wpdb in a class or something like that...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Nikolaev, 2019-08-01
@Heian

class User{

  private $id;
  private $data_from_wbdb_get_results;

  publick function __construct($id){
    global $wpdb;
    $this->id = $id;
    $this->data_from_wbdb_get_results = $wpdb->get_results("SELECT * FROM table WERE id=$this->id");
  }

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question