I am not new to OOP I am tidying up a very large site of mine and trying to create a much better structure (more in keeping with the whole point of OOP). I have decided to use mysqli (please don't suggest PDO as I have decided againast it) which is new to me and am a little confused to say the least with a problem I am having with a basic select * query.
In my database class I have the following functions (only the ones relevant to this thread):
public function selectAll($table, $fields = '*', $where = NULL, $order = NULL, $limit = NULL, $offset = NULL)
{
$sql = 'SELECT '.$fields.' FROM '.$table;
if ($where != NULL) {
$sql .= ' WHERE '.$where;
}
if ($order != NULL) {
$sql .= ' ORDER BY '.$order;
}
if ($limit != NULL) {
$sql .= ' LIMIT '.$limit;
}
if ($offset != NULL) {
$sql .= ' OFFSET '.$offset;
}
return $this->doQuery($sql);
}
public function doQuery($sql)
{
$query = $this->dbh->query($sql);
$last_query = $sql;
try {
if (!$query) {
throw new CustomException("Query failed: ".$last_query);
} else {
$array['result'] = $query->fetch_assoc();
$array['total_rows'] = $query->num_rows;
return $array;
}
} catch (CustomException $e) {
echo $e->getErrorReport();
}
}
In my child class, I have the following:
public function listShows($status = 'All') {
switch ($status) {
case 'All':
/* Get all shows in database */
$where = "";
$order = "name ASC";
break;
// All other cases removed to keep things short
}
return $this->selectAll($this->table, '*', $where, $order);
}
And in my index.php test file I have:
$s = new Shows;
print_r($s->listShows());
// The print_r is just for my testing purposes and I have tried it within the classes - all the same results
When I open index.php I get an array but it is only an array of all the details for the first row returned by the query and yet it prints out a total of 33 rows from the num_rows (which is the correct number of rows in the table).
If I try and run the results from fetch_assoc through a while loop, I just get a looping list of the first record (i.e. no other records from the table and a never ending list of the same details).
I have echo'd the SQL and it is fine and rund correctly in the DB and I am getting no other errors
What am I missing?