example: add example of how to use Db get_one() & get_many() helpers

This commit is contained in:
Benjamin Renard 2023-03-02 19:22:21 +01:00
parent 2584a59123
commit becb9ad366
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC

View file

@ -22,22 +22,16 @@ $db = new Db(
function get_items($orderby='id', $raw_values=false) {
global $db;
try {
$query = $db -> fpdo -> from('item')
-> orderBy($orderby);
$info = $db -> get_many('item', null, null, $orderby);
if (!is_array($info))
return;
if ($raw_values)
return $info;
$result = $query -> execute();
if ($result !== false) {
$info = $result -> fetchAll();
if ($info === false)
return null;
if ($raw_values)
return $info;
$items = array();
foreach ($info as $item)
$items[$item['id']] = $db -> format_row_info($item, array('date'));
return $items;
}
$items = array();
foreach ($info as $item)
$items[$item['id']] = $db -> format_row_info($item, array('date'));
return $items;
}
catch (Exception $e) {
Log :: error("Error retreiving items info from database : ".$e->getMessage());
@ -48,19 +42,15 @@ function get_items($orderby='id', $raw_values=false) {
function get_item($id, $raw_values=false) {
global $db;
try {
$query = $db -> fpdo -> from('item')
-> where('id', $id);
$info = $db -> get_one('item', array('id' => $id));
$result = $query -> execute();
if ($result !== false) {
$info = $result -> fetch();
if ($info === false)
return null;
if ($raw_values)
return $info;
if (!is_array($info))
return false;
return $db -> format_row_info($info, array('date'));
}
if ($raw_values)
return $info;
return $db -> format_row_info($info, array('date'));
}
catch (Exception $e) {
Log :: error("Error retreiving item #$id info from database : ".$e->getMessage());