diff --git a/dblayer.php b/dblayer.php index 43dd0ee..7076b03 100644 --- a/dblayer.php +++ b/dblayer.php @@ -103,29 +103,40 @@ class dblayer extends PDO } /** 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="=") + operator and the associated select value + $select = array (array ($key, $val, $operator)) + $key=>column, $val=>value to found, $operator=>'LIKE', =... + */ + function read ($select=null) { 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 && $selectval !== NULL) - $req .= " WHERE $selectkey $selectop :selectkey"; + if ($select !== null) + { + $req .= " WHERE "; + foreach ($select as $n=>$s) + { + if ($n > 0) + $req .= " AND "; + if (!isset ($s[2])) + $s[2] = "="; + $req .= " ".$s[0]." ".$s[2]." :".$s[0]; + } + } if ($this->debug) echo "DEBUG : $req\n"; $st = $this->db->prepare ($req); - if ($selectkey !== NULL && $selectval !== NULL) + if ($select !== NULL) { - if ($this->debug) echo "DEBUG BIND : selectkey->". - var_export ($selectkey, TRUE)."\n"; - $st->bindValue (":selectkey", $selectval); + foreach ($select as $s) + { + if ($this->debug) echo "DEBUG BIND : ".$s[0]."->". + var_export ($s[1], TRUE)."\n"; + $st->bindValue (":".$s[0], $s[1]); + } } $st->execute (); $res = array ();