There was a question - I am developing a news form for the site and in the same way I try to learn OOP.

$obj=new news(); $result = $obj->get_news(); while($output = mysqli_fetch_assoc($result)) { echo '<div class="product"> <div class="product_image"> <image src="/images/'.$output["id"].'.jpg" /> </div> <h2> '.$output["name"].' </h2> <div class="product_price"> '.$output["price"].' $. </div> <div class="product_buy"> <a href="/">korzina</a> </div> </div>'; } 

and the function of news:

 require 'displaynews.php'; public $db; public function __construct(){ $this->db = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE); if(mysqli_connect_errno()) { echo "Error: Could not connect to database."; exit; } public function get_news(){ $query = 'SELECT name, price FROM news'; $result = $this -> mysqli -> query($query); return $result; } Fatal error: Call to a member function query() on a non-object in 

Swears at the line:

  $result = $this -> mysqli -> query($query); 

Help to understand, please.

Closed due to the fact that off-topic participants Mike , cheops , aleksandr barakin , D-side , zRrr 11 May '16 at 14:10 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - Mike, cheops, aleksandr barakin, D-side, zRrr
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • You would lead. It's not clear where and how you initialize mysqli - Mike
  • include "db_config.php"; public function __construct () {$ this-> db = new mysqli (DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE); if (mysqli_connect_errno ()) {echo "Error: Could not connect to database."; exit; }} - newuser
  • @newuser edit your question and paste this code in there - Vasily Barbashev
  • I do not understand why you are doing $this->db = ... during initialization, but you are accessing as $this->mysqli you decide on the variable name - Mike
  • @Mike thank! it all worked. The topic is closed. - newuser

0