Database: add fetch_object, queryObject, queryObjects methods

This commit is contained in:
Aaron van Geffen 2023-06-12 12:49:22 +02:00
parent 814a1f82f6
commit 978d6461c5

View File

@ -59,6 +59,14 @@ class Database
return mysqli_fetch_assoc($resource);
}
/**
* Fetches a row from a given recordset, encapsulating into an object.
*/
public function fetch_object($resource, $class)
{
return mysqli_fetch_object($resource, $class);
}
/**
* Fetches a row from a given recordset, using numeric keys.
*/
@ -341,6 +349,41 @@ class Database
return $db_string;
}
/**
* Executes a query, returning an object of the row it returns.
*/
public function queryObject($class, $db_string, $db_values = [])
{
$res = $this->query($db_string, $db_values);
if (!$res || $this->num_rows($res) == 0)
return null;
$object = $this->fetch_object($res, $class);
$this->free_result($res);
return $object;
}
/**
* Executes a query, returning an array of objects of all the rows returns.
*/
public function queryObjects($class, $db_string, $db_values = [])
{
$res = $this->query($db_string, $db_values);
if (!$res || $this->num_rows($res) == 0)
return [];
$rows = [];
while ($object = $this->fetch_object($res, $class))
$rows[] = $object;
$this->free_result($res);
return $rows;
}
/**
* Executes a query, returning an array of all the rows it returns.
*/