Uncaught Error: Call to a member function execute()

Codes here !

Moderators: egami, macek, gesf

Post Reply
joaomont02
New php-forum User
New php-forum User
Posts: 1
Joined: Sat Nov 13, 2021 3:36 pm

I am in trouble with one part of my code. It seems to be working pretty fine on the localhost server, but when I upload the project to the webserver it has one error that I can't fix.

It says:
Fatal error: Uncaught Error: Call to a member function execute() on boolean in /home2/myserver/public_html/site/class/Articles.php:27 Stack trace: #0 /home2/myserver/public_html/site/noticias.php(14): Articles->getArticles() #1 /home2/myserver/public_html/site/index.php(3): include('/home2/baconboa...') #2 {main} thrown in /home2/myserver/public_html/site/class/Articles.php on line 27
Here's my code for getArticles() function and the error on line 27:

Code: Select all

public function getArticles(){

		$query = '';
		if($this->id) {		
			$query = " AND p.id ='".$this->id."'";
		}
		$sqlQuery = "
			SELECT p.id, p.title, p.descricao, p.message, p.category_id, p.userid, p.imagem, u.first_name, u.last_name, p.status, p.created, p.updated, c.name as category
			FROM ".$this->postTable." p
			LEFT JOIN ".$this->categoryTable." c ON c.id = p.category_id
			LEFT JOIN ".$this->userTable." u ON u.id = p.userid
			WHERE p.status ='published' $query ORDER BY p.id DESC";
			
		$stmt = $this->conn->prepare($sqlQuery);		
		$stmt->execute();
		$result = $stmt->get_result();	
		return $result;
	}
my index has:

Code: Select all

<?php 
    include('topo.php');
    include('noticias.php'); ?>
</div>
</body>
</html>
and noticias.php part is:

Code: Select all

<?php 
include_once 'config/Database.php';
include_once 'class/Articles.php';
include_once 'painel/class/User.php';

$database = new Database();
$db = $database->getConnection();

$article = new Articles($db);
$article->id = 0;
$result = $article->getArticles();
$user = new User($db);
?>
I have tried a lot of things to solve the problem, but I DON'T REALLY KNOW HOW TO DO IT. It's my first time working with a server besides the local server so please, I beg you, help meeeeeeeeeeeeee
simonbrahan
php-forum Super User
php-forum Super User
Posts: 160
Joined: Mon Jun 08, 2020 2:00 am
Contact:

This error means you are trying to call a method on something that isn't a class; it's a boolean. If you look at the documentation for the prepare() function, you'll see that it can return a boolean "false" in the event of an error. This is what's happened. Take a look at your connection class to find out how to see what the error is.
Post Reply