'Student Project - Fatal Error: Uncaught Error: Call to a member function prepare() on null in php
I'm working on a student project. I'm building an e-commerce website to sell soaps, and the error message I'm getting is the following.
Fatal error: Uncaught Error: Call to a member function prepare() on null in /home/customer/www/myname.com/public_html/projects/call-of-the-river/objects/soap.php:41 Stack trace: #0 /home/customer/www/myname.com/public_html/projects/call-of-the-river/index.php(11): Soap->readAll() #1 {main} thrown in /home/customer/www/mattroddy.com/public_html/projects/call-of-the-river/objects/soap.php on line 41
Code from Index.php lines 1-17:
// Imports the database and the class files
include_once 'config/database.php';
include_once 'objects/soap.php';
// Creates a database object based on that class
$database = new Database();
// Creates a conn with the database object, and is stored in the var
$conn = $database->getConnection();
$sham = new Soap($conn);
$stmt = $sham->readAll();
// This will set up the page title string in a var that header.php will use:
$pageTitle = 'Home';
// Brings in the HTML for the top of the page
include_once('includes/header.php');
Code from Soap.php lines 33-46:
/**
* This method reads all the row data from the table
*
* @return an object (like an array) full of row data
*/
function readAll()
{
// Prepares a query using that connection, store it in a var $stmt
$stmt = $this->conn->prepare('SELECT * FROM '.$this->tableName);
// Runs the query, which causes $stmt to become full of the retrieved data.
$stmt->execute();
// Send the data back to where the function was called
return $stmt;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
