From ad830056b1787573de1dc885b4edd4ce66fd8237 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Thu, 13 Mar 2014 19:00:05 +0000 Subject: [PATCH] Add correct read support (filter without value, bad key assignement) git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@1218 bf3deb0d-5f1a-0410-827f-c0cc1f45334c --- dblayer.php | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/dblayer.php b/dblayer.php index da34b9d..43dd0ee 100644 --- a/dblayer.php +++ b/dblayer.php @@ -46,7 +46,9 @@ class dblayer extends PDO // TODO !! /** Create automatic creation of $fields from .schema of sqlite3/ - show create table `NomTable`; for MySQL*/ + show create table `NomTable`; for MySQL + SQLite3 : PRAGMA TABLE_INFO('yourtable'); + MYSQL : SHOW COLUMNS FROM yourtable;*/ /** Connection to the database engine See http://fr2.php.net/manual/en/pdo.construct.php for the $dsn format */ @@ -100,24 +102,30 @@ class dblayer extends PDO return $this->db->lastInsertId(); } - /** Read all the table or only one line defined by the primary key */ - function read ($selectkey=null) + /** Read all the table or only one line defined by the select key, the select + operator and the associated select value */ + function read ($selectkey=null, $selectval=null, $selectop="=") { if ($this->db === null) throw new Exception ("Database not connected"); + if ($selectop !== "=" && $selectop !== "like" && $selectop !== null) + throw new Exception ("Invalid selectop"); + if ($selectkey !== null && + ! in_array ($selectkey, array_keys ($this->fields))) + throw new Exception ("Unknown key field provided"); $req = "SELECT "; $req .= implode (",", array_keys ($this->fields)); $req .= " FROM `".$this->table."`"; - if ($selectkey !== NULL) - $req .= " WHERE $this->primary=:primary"; + if ($selectkey !== NULL && $selectval !== NULL) + $req .= " WHERE $selectkey $selectop :selectkey"; if ($this->debug) echo "DEBUG : $req\n"; $st = $this->db->prepare ($req); - if ($selectkey !== NULL) + if ($selectkey !== NULL && $selectval !== NULL) { - if ($this->debug) echo "DEBUG BIND : primary->". + if ($this->debug) echo "DEBUG BIND : selectkey->". var_export ($selectkey, TRUE)."\n"; - $st->bindValue (":primary", $selectkey); + $st->bindValue (":selectkey", $selectval); } $st->execute (); $res = array ();