Click here to Skip to main content
15,746,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Everyone, I'm gettin
Fatal error
: Uncaught Error: Call to a member function execute() on bool in C:
in my php file please how do I solve this

What I have tried:

PHP
class Food {	
   
	private $foodItemsTable = 'food_items';	
	private $conn;
	
	public function __construct($db){
        $this->conn = $db;
    }	    
	
	public function itemsList(){		
		$stmt = $this->conn->prepare("SELECT id, name, price, description, images, vendor_name, vendor_address, status FROM ".$this->foodItemsTable);				
		$stmt->execute();			
		$result = $stmt->get_result();		
		return $result;	
	}
}
Posted
Updated 11-May-23 12:45pm
v2

See here: PHP: PDO::prepare - Manual[^]
Quote:
Return Values
If the database server successfully prepares the statement, PDO::prepare() returns a PDOStatement object. If the database server cannot successfully prepare the statement, PDO::prepare() returns false or emits PDOException (depending on error handling).
So for some reason prepare is returning false
And the reason why is probably that dot...
SQL
status FROM ".$this->foodItemsTable)
             ^
             |
 
Share this answer
 
Comments
Jeffersonballer 6-May-23 16:13pm    
Thank you.. After removing the (dot) I still get another error (syntax error , unexpected $this (T_VARIABLE),expecting')' in line 12)
Dave Kreskowiak 6-May-23 20:59pm    
By looking at the documentation for PHP, it seems the concatentation operator requires a space before and after it:
$stmt = $this->conn->prepare("SELECT id, name, price, description, images, vendor_name, vendor_address, status FROM " . $this->foodItemsTable);
Jeffersonballer 6-May-23 23:24pm    
I will try this and let you know if it works.. Thanks
Sorry this my original Code:

PHP
class Food {    
   
    private $foodItemsTable = 'food_items'; 
    private $conn;
    
    public function __construct($db){
        $this->conn = $db;
    }       
    
    public function itemsList(){        
        $stmt = $this->conn->prepare("SELECT id, name, price, description, images, vendor_name, vendor_address, status FROM food_items ORDER BY rand()");               
        $stmt->execute();           
        $result = $stmt->get_result();      
        return $result; 
    }
}
 
Share this answer
 
v2
Yes I've figure it out.. The issues was with Database Column...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900