>**Fatal error**: Uncaught Error: Call to a member function `select()` on `string` in C:\OSPanel\domains\phplog\index.php:12 *Stack trace:* #0 {main} thrown in C:\OSPanel\domains\phplog\index.php on line 12 

Here is the code index.php

 <?php include 'config/config.php'; ?> <?php include 'libraries/database.php'; ?> <?php include 'includes/header.php'; ?> <?php //Create DB Object $db = 'database'; //Create Query $query = "SELECT * FROM posts"; //Run Query $posts = $db->select($query); ?> <?php if($posts) : ?> <div class="blog-post"> <h2 class="blog-post-title">Another blog post</h2> <p class="blog-post-meta">December 23, 2013 by <a href="#">Jacob</a></p> <p>text.</p> <a class="readmore" href="post.php?id=1">read more</a> </div><!-- /.blog-post --> <div class="blog-post"> <h2 class="blog-post-title">Another blog post</h2> <p class="blog-post-meta">December 23, 2013 by <a href="#">Jacob</a></p> <p>text.</p> <a class="readmore" href="post.php?id=1">read more</a> </div><!-- /.blog-post --> <?php else : ?> <p>Thete are no posts yet</p> <?php endif; ?> <?php include 'includes/footer.php'; ?> 

Here is the database.php code

 <?php class database{ public $host = db_host; public $username = db_username; public $password = db_pass; public $db_name = db_name; public $link; public $error; /* * Class Constructor */ public function __construct(){ //Call Connect Function $this->connect(); } /* * Connector */ private function connect() { $this->link = new mysqli($this->host, $this->username, $this->password, $this->db_name); if(!$this->link) { $this->error = "Connection Failed: ".$this->link->connect_error; return false; } } /* * Select */ public function select($query) { $result = $this->link->query($query) or die($this->link- >error.__LINE__); if($result->num_rows > 0){ return $result; } else { return false; } } /* * Insert */ public function insert($query) { $insert_row = $this->link->query($query) or die($this->link- >error.__LINE__); //Validate Insert if($insert_row) { header("Location: index.php?msq=".urlencode('Record insert')); exit(); } } /* * Update */ public function update($query) { $update_row = $this->link->query($query) or die($this->link- >error.__LINE__); //Validate update if($update_row) { header("Location: index.php?msq=".urlencode('Record update')); } } } ?> 

And here is the connection code itself config.php database

 <?php define('db_host', 'localhost'); define('db_user', 'root'); define('db_pass', ''); define('db_name', 'blog'); ?> 
  • four
    $ db you have a string (text), and not an object to work with the database - Mike
  • <? php // Create DB Object $ db = 'database'; // Create Query $ query = 'SELECT * FROM posts'; // Run Query $ posts = $ db-> select ($ query); ?> - Daniil Krylov
  • which confirms the first comment again - teran
  • eight
    Yeah, you for some reason, as a connection to the database, why does the simple inscription 'database' appear? You might as well write a β€œplane” on the asphalt and expect it to fly - Mike
  • one

1 answer 1

The error is due to the fact that instead of creating an object, you assign a string. You need to fix index.php as follows:

 <?php //ΡƒΠ±Ρ€Π°Π» лишниС php-Ρ‚Π΅Π³ΠΈ include 'config/config.php'; include 'libraries/database.php'; include 'includes/header.php'; //Create DB Object $db = new database(); //здСсь создаСм ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ класса database, ΠΊΠ°ΠΊ ΠΈ сказано ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠ΅ΠΌ Π²Ρ‹ΡˆΠ΅ //Create Query $query = "SELECT * FROM posts"; //Run Query $posts = $db->select($query); ?> <?php if($posts) : ?> <div class="blog-post"> <h2 class="blog-post-title">Another blog post</h2> <p class="blog-post-meta">December 23, 2013 by <a href="#">Jacob</a></p> <p>text.</p> <a class="readmore" href="post.php?id=1">read more</a> </div><!-- /.blog-post --> <div class="blog-post"> <h2 class="blog-post-title">Another blog post</h2> <p class="blog-post-meta">December 23, 2013 by <a href="#">Jacob</a></p> <p>text.</p> <a class="readmore" href="post.php?id=1">read more</a> </div><!-- /.blog-post --> <?php else : ?> <p>Thete are no posts yet</p> <?php endif; ?> <?php include 'includes/footer.php'; ?> 
  • Then it gives 4 errors. Warning: Use of undefined code in C: \ OSPanel \ domains \ phplog \ index.php on 6
  • As I understood the problems with the php version, since I have version 7.2 - Daniil Krylov
  • In the database.php file, replace the line with public $ username = db_username; on public $ username = db_user; either correct the constant in the config - Sergey Shitikov
  • Woo, thank you very much, but he racked his head) - Daniil Krylov
  • Very helpful !!! - Daniil Krylov