// Класс PDO легко можно расширить и получить при этом // дополнительные фичи в виде простой отладки и работы с хешами define('DSN', 'sqlite:'.dirname(__FILE__).'/data.db'); define('DEBUG_SQL', false); class EPDO extends PDO { public function exec($sql) { if (DEBUG_SQL) echo "SQL> ".$sql."n"; return parent::exec($sql); } public function query($sql) { if (DEBUG_SQL) echo "SQL> ".$sql."n"; return parent::query($sql); } public function now() { $now = date('Y-m-d H:i:s'); $sql = "SELECT datetime('now','localtime')"; foreach($this->query($sql) as $row) { $now = $row[0]; break; } return $now; } public function insert($data, $table) { $k = implode(',',array_keys($data)); $v = ':'.implode(',:',array_keys($data)); $sql = sprintf( 'INSERT INTO %s (%s) VALUES (%s)', $table, $k, $v ); $c = $this->prepare($sql); $c->execute($data); return $this->lastInsertId(); } public function update($data, $table, $where='') { $u = array(); foreach($data as $k=>$v) $u[] = $k.'=:'.$k; $u = implode(',',$u); $sql = sprintf( 'UPDATE %s SET %s', $table,$u ); if ($where!='') $sql .= " WHERE ".$where; $c = $this->prepare($sql); $c->execute($data); } public function row($table,$where,$select="*") { $sql = "SELECT $select FROM $table".($where!=''?" WHERE $where LIMIT 1":''); foreach($this->query($sql) as $row) { return $row; } return null; } public function exists($sql) { $f = false; foreach($this->query($sql) as $row) { $f=true; break; } return $f; } } // EPDO function db_connect($dsn=DSNX,$u='root',$p='d0min0') { static $db = array(); if (!isset($db[$dsn])) { $db[$dsn] = new EPDO($dsn,$u,$p); $db[$dsn]->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "PRAGMA synchronous=OFF; PRAGMA journal_mode=MEMORY; PRAGMA temp_store=MEMORY; "; $db[$dsn]->exec($sql); } return $db[$dsn]; }
PHP Справочник v0.05 © 2007-2024 Igor Salnikov aka SunDoctor