From becb9ad366754128230e46dc6d023ffb4f3d7b33 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Thu, 2 Mar 2023 19:22:21 +0100 Subject: [PATCH] example: add example of how to use Db get_one() & get_many() helpers --- example/includes/db.php | 42 ++++++++++++++++------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/example/includes/db.php b/example/includes/db.php index cbeae5c..0e87c81 100644 --- a/example/includes/db.php +++ b/example/includes/db.php @@ -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());