So here's a little background to help with what I'm trying to accomplish (be mindful I'm also fairly new to MySQL):
I'm creating a dynamically built navigation for a website using PHP and MySQLi. I've created a class called Database where I've got helper functions, such as connect() for connecting to the database, and the like. Then I have another class called Navigation which extends Database, and there I'm creating helper functions to do specific things for the database that holds my navigation links, such as selecting and inserting and so on.
Now, the following code works flawlessly when working with my data:
function get() {
$statement = $this->connect()->prepare(
"SELECT * FROM `category` ORDER BY `parent_id`, `sort_order`, `category_name`"
);
$statement->execute();
return $statement->get_result();
}
Later, I retrieve my data using:
$navigation_library = new Navigation();
$navigation = $navigation_library->get();
Finally, I throw the data it into some arrays I've set up:
while ($row = $navigation->fetch_assoc()) {
$category['categories'][$row['category_id']] = $row;
$category['parent_categories'][$row['parent_id']][] = $row['category_id'];
}
I've got get()
for SELECT, add()
for INSERT queries, edit()
for UPDATE queries, etcetera. Doing it that way, though, I'm reusing code by going through the prepared statements over and over again for each of those... and while that's perfectly fine, I'm trying to keep it cleaner by doing the prepared statements like the following:
function perform($sql) {
try {
$statement = $this->connect()->prepare($sql);
$statement->execute();
} catch (Exception $exception) {
$this->error = $exception;
}
return $statement->get_result();
}
Then pulling the data in get()
like this:
$this->perform("SELECT * FROM `category` ORDER BY `parent_id`, `sort_order`, `category_name`");
This throws an exception later when the later code gets to the $navigation->fetch_assoc()
part and gives me the Call to a member function fetch_assoc() on null
error. I've been working almost non-stop for days and I'm feeling like I'm missing something that should be crazy obvious... but I'm stumped, as it doesn't particularly make sense: obviously there's no data there... yet there's no error in my error
variable I have setup to collect the Exception result in try
.
So now I come to you, DaniWeb denizens, to hopefully save the rest of the hair on my head. Please?