OT
Kit: Promin, ale tve pindy na mysql funkce jsou spatne. Ty funkce spravne funguji. Kdezdo pdo jsou naprosto nedodelane, neintuitivni. Sesmolene na kolene. Jedine, co je tam jako vylepseni je pouze to, ze to umi prepisovat promenne do sql prikazu. Jinak je to hnus, fujka. Treba odchytavat chybove stavy. Prosel sql dotaz, ano, ne? S jakou chybou? Prosel sql dotaz a vratil vysledek 0 radku, 1 radek, vice radku? Prosel sql dotaz mazani radku ano, ne? Kolik smazal radku?
Tohle je muj pokus udelat si nejakou univerzalni knihovnu.
<?php
class classSql
{
public $conn = null;
//public $db = null;
public $sth = null;
public $sth_exec = null;
private $cfg = null;
//private $log = null;
function __construct($params)
{
$this->createConfig($params);
$this->connect();
$this->query("SET NAMES utf8");
// $this->log = array(
// 'logdate'=>'logdate',
// 'loguser'=>'loguser'
// );
}
function __destruct()
{
$this->disconnect();
}
function createConfig($params)
{
$cfg = array(
'host' => '',
'user' => '',
'psw' => '',
'db' => '',
'debug' => false
);
foreach($cfg as $key=>$value)
{
$cfg[$key] = isset($params[$key]) ? $params[$key] : $value;
}
//$cfg['query'] = ;
$this->cfg = $cfg;
// if ($this->cfg['debug'])
// {
// $this->query = function ($query) {$this->queryDebug($query);};
// }
// else {
// $this->query = function ($query) {$this->querySafe($query);};
// }
//var_dump($this->query);
// return (!$this->cfg['debug']) ? $this->query1($query) : $this->query2($query);
//echo $this->query = $this->($this->query);
// $this->query = $this->{$this->query};
//($query);
//function($query) {}
}
function connect()
{
//phpinfo();
$this->conn = new PDO('mysql:host='.$this->cfg['host'].';dbname='.$this->cfg['db'], $this->cfg['user'], $this->cfg['psw'])
or $this->error();
// $x = $this->cfg['query'];
// $x = $this->{$x};
// $x($query);
}
public function disconnect()
{
if (isset($this->conn))
{
$this->conn = null;
}
}
// function free($result)
// {
// mysql_free_result($result);
// }
public function lastInsertId()
{
return ($this->sth_exec) ? $this->conn->lastInsertId() : null;
}
public function query($query)
{
// $x = $this->cfg['query'];
// $x = $this->{$x};
// $x($query);
if ($this->cfg['debug'])
{
return $this->queryDebug($query);
}
else {
return $this->querySafe($query);
}
}
public function querySafe($query)
{
$this->sth = $this->conn->prepare($query);
$this->sth_exec = $this->sth->execute();
return $this->sth;
}
public function queryDebug($query)
{
echo "\n<div class=\"query\">query = ".$query."</div>";
return $this->querySafe($query)
or die('Chyba pdo_query: ' . $this->sth->errorCode().' '.implode(" - ",$this->sth->errorInfo()));
}
public function fetch($sth=null)
{
// var_dump( $this->sth->fetch(PDO::FETCH_ASSOC)); //$sth
$sth = !$sth ? $this->sth : $sth;
return $sth->fetch(PDO::FETCH_ASSOC); //$sth
}
// public function numRows($sth)
// {
// return $sth->query('SELECT FOUND_ROWS()')->fetchColumn();
// }
public function createInsert($_table,$_data,$_idname='')
{
if ($_idname!='' && isset($_data[$_idname])) {unset($_data[$_idname]);}
$keys = array();
$values = array();
foreach ($_data as $key=>$value)
{
$keys[] = $this->escapeKey($key);
$values[] = $this->escape($value);
}
return "INSERT INTO ".$this->escapeKey($_table)." (".implode(", ",$keys).") VALUES (".implode(", ",$values).")";
}
public function createUpdate($_table,$_data,$_idname)
{
$_id = array($_idname=>$_data[$_idname]);
unset($_data[$_idname]);
$values = array();
$where = array();
foreach ($_data as $key=>$value)
{
$values[] = $this->escapeKey($key)."=" . $this->escape($value);
}
foreach ($_id as $key=>$value)
{
$where[] = $this->escapeKey($key)."=" . $this->escape($value);
}
return "UPDATE ".$this->escapeKey($_table)." SET ".implode(", ",$values)." WHERE ".implode(' AND ',$where);
}
public function createDelete($_table,$_id)
{
$where = array();
foreach ($_id as $key=>$value)
{
$where[] = $this->escapeKey($key)."=" . $this->escape($value);
}
return "DELETE FROM ".$this->escapeKey($_table)." WHERE ".implode(' AND ',$where);
}
public function escape($value='')
{
return $this->conn->quote($value);
}
public function unEscape($value='')
{
return $this->conn->quote($value);
}
public function escapeKey($value='')
{
return "`".str_replace("`","``",$value)."`";
}
public function escapeSelect($value='')
{
$m = strlen($value);
$value = preg_replace('~UPDATE|REPLACE|DELETE|CREATE~i','',$value);
$n = strlen($value);
return $m==$n ? $value : '';
}
};
/*
$sql = new classSql(array(
'host' => 'localhost',
'user' => 'root',
'psw' => '',
'db' => 'urb_news',
'debug' => true
));
*/
?>
Ale v konecnem dusledku to pak v programu resim takto
$query2 = selectQuery($table,$where2);
$result2 = $sql->query($query2);
if ($sql->sth_exec && $result2->rowCount()>0)
{
while(($row2 = $sql->fetch())!==false)
{
$x = 0;
foreach($row2 as $value2)
{
$bool2 &= $value2==$row[$x];
++$x;
}
break; // first row
}
$query = $sql->createDelete('schedule',$data_delete);
$result = $sql->query($query);
if ($sql->sth_exec && $result->rowCount()>0)
{$notice[] = sprintf($lng['all']['delete_true'],$result->rowCount()); $bool_join=true;}
else {$notice[] = $lng['all']['delete_false'].keyString($data,$id_name);}
Treba casto fetchuji vic resultu. Dalo mi to asi 2 dny googlovani, nez jsem prisel na to, co z toho je vlastne result. Hlavne, ze tam maji spesl nelogicke nazvy objektu jako sth. Co je sth? Zkratka? Proc je tam zkratka a ne kratke srozumitelne slovo? To delal nejaky vul!
Takze budu dal zaryte python a php-pdo "hater", protoze v necem tak strasnem se neda programovat.