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) { function get_items($orderby='id', $raw_values=false) {
global $db; global $db;
try { try {
$query = $db -> fpdo -> from('item') $info = $db -> get_many('item', null, null, $orderby);
-> orderBy($orderby); if (!is_array($info))
return;
if ($raw_values)
return $info;
$result = $query -> execute(); $items = array();
if ($result !== false) { foreach ($info as $item)
$info = $result -> fetchAll(); $items[$item['id']] = $db -> format_row_info($item, array('date'));
if ($info === false) return $items;
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;
}
} }
catch (Exception $e) { catch (Exception $e) {
Log :: error("Error retreiving items info from database : ".$e->getMessage()); 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) { function get_item($id, $raw_values=false) {
global $db; global $db;
try { try {
$query = $db -> fpdo -> from('item') $info = $db -> get_one('item', array('id' => $id));
-> where('id', $id);
$result = $query -> execute(); if (!is_array($info))
if ($result !== false) { return false;
$info = $result -> fetch();
if ($info === false)
return null;
if ($raw_values)
return $info;
return $db -> format_row_info($info, array('date')); if ($raw_values)
} return $info;
return $db -> format_row_info($info, array('date'));
} }
catch (Exception $e) { catch (Exception $e) {
Log :: error("Error retreiving item #$id info from database : ".$e->getMessage()); Log :: error("Error retreiving item #$id info from database : ".$e->getMessage());