PHP Fatal error

<?php include 'config/config.php'; ?> <?php include 'libraries/database.php'; ?> <?php include 'includes/header.php'; ?> <?php //Create DB Object $db = new Database(); // Create query $query = "SELECT * FROM posts"; // Run Query $posts = $db->select($query); ?> <?php if($posts) : ?> <?php while($row = $posts->fetch_assoc()) : ?>
    <div class="blog-post">
        <h2 class="blog-post-title"><?php echo $row['title']; ?></h2>
        <p class="blog-post-meta">January 1, 2014 by <a href="#">rdamorei</a></p>
        <h2 class="blog-post-title"></h2>
        <h2 class="blog-post-title"><?php echo $row['body']; ?></h2>
      <a class="readmore" href="post.php?id=1">Read more</a>
    </div><!-- /.blog-post -->
<?php endwhile; ?>
<?php else : ?>

There are no posts yet!

<?php endif; ?> <?php include 'includes/footer.php'; ?>

database

<?php class Database { public $host = DB_HOST; public $username = DB_USER; 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 = "Conection Faile: ".$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?msg=$".urlencode('Record Added')); exit(); } else { die('Error: ('.$this->link->errno.') '.$this->link->error); } } /* * Update */ public function update($query){ $update_row = $this->link->query($query) or die($this->link->error.__LINE__); // Validate insert if($update_row){ header("Location: index.php?msg=$".urlencode('Record Updated')); exit(); } else { die('Error: ('.$this->link->errno.') '.$this->link->error); } } /* * Delete */ public function delete($query){ $delete_row = $this->link->query($query) or die($this->link->error.__LINE__); // Validate insert if($delete_row){ header("Location: index.php?msg=$".urlencode('Record Delete')); exit(); } else { die('Error: ('.$this->link->errno.') '.$this->link->error); } } } Izbacuje mi: Fatal error: Call to a member function query() on null in Database.php on line 38 Ako obrisem ovaj upit na bazu i index.php onda mi neizbacuje grešku ako probam izvršiti upit onda mi izbacuje fatal error

Jel iz te greske ne mozes zakljuciti zasto se dogadja? Greska je poprilicno citljiva.

Pokusavas pozvati metodu query() na null vrijednosti, a metode se mogu zvati na objektima.

Ako pogledas liniju koju ti veli (38), vidis da se pokusava pozvati
$this->link->query(), dakle $this->link ti je null.

Ako pogledas constructor metodu, vidis da tamo nesto ne stima i da se ne moze spojiti na bazu. Provjeri credentialse, odnosno tamo gdje se definiraju DB_HOST, DB_USER, DB_PASS i DB_NAME konstante.


Copyright © 2020 WM Forum - AboutContact - Sponsored by: Mydataknox & Profit Monkey