better select

This commit is contained in:
Malte Schulze Hobeling 2023-01-18 10:51:28 +01:00
parent b93e0c8e5a
commit e666991b60

View File

@ -102,27 +102,30 @@ class BancaDati {
/** /**
* einheitliche Select Funktion * einheitliche Select Funktion
* kann sortiert werden durch ["by"]=>"col" und ["order"]=>"ASC|DESC"
* @param string $table * @param string $table
* @param array $where ["column"]=>"value" es wird mit LIKE verglichen und mit AND verbunden * @param array $where ["column"]=>"value" es wird mit LIKE verglichen und mit AND verbunden
* @param array|null $order ["by"]=>"column"; ["order"]=>"ASC|DESC"
* @return void * @return void
* @author Malte Schulze Hobeling * @author Malte Schulze Hobeling
*/ */
public function select(string $table, array $where, array $order = null){ public function select(string $table, array $where){
$whereString = ""; $whereString = "";
$orderString = "";
if(isset($where["by"])){
$orderString = " ORDER BY " . $where["by"];
unset($where["by"]);
if(isset($where["order"])){
$orderString .= " " . $where["order"];
unset($where["order"]);
}
}
foreach ($where as $col => $v) { foreach ($where as $col => $v) {
if($whereString != ""){ if($whereString != ""){
$whereString .= " AND "; $whereString .= " AND ";
} }
$whereString .= "`" . $col . "` LIKE '" . $v . "'"; $whereString .= "`" . $col . "` LIKE '" . $v . "'";
} }
$sql = "SELECT * FROM ".$table." WHERE ".$whereString.";"; $sql = "SELECT * FROM ".$table." WHERE ".$whereString.$orderString.";";
if(isset($order["by"])){
$sql .= " ORDER BY ".$order["by"];
}
if(isset($order["order"])){
$sql .= $order["order"];
}
try { try {
return $this->pdo->query($sql)->fetch(); return $this->pdo->query($sql)->fetch();
}catch (PDOException $e){ }catch (PDOException $e){