Nn, ja nemyslim odtrhnout dotaz z mista pouziti. Jen si proste na sql postavit vlastni tridu. Jestli je uvnitr prikaz pdo nebo mysql je pak jedno. Kdyz to budes chtit pak upgradovat, tak staci prepsat tu tridu. Sice je to mozna trochu pracnejsi to odladit nez resit na miste, ale jevi se mi to elegantnejsi.
Treba, ja ted mam neco takoveho. Neni to asi jeste vsechno vyladne. Ale, kde to pouzivam, tam mi to zatim staci.
<?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 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>";
$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 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
));
*/
?>