<?php/** * @author Jerome Izard * @version 1.0 * @package package_name */require_once('class.benchmark.php');class Mysql{ private $id = NULL; private $result = NULL; public $query = 0; public $bench = 0; public function __construct() { $this->bench = new benchmark(); $this->bench->start(); $this->connect('localhost', 'root', ''); $this->selectDb('elvetika'); } public function connect($host, $user, $pwd) { $this->id = mysql_connect($host, $user, $pwd); if (!$this->id) throw new MyException('Erreur MySQL : impossible de se connecter.'); } public function selectDb($db) { if (!mysql_select_db($db, $this->id)) throw new MyException('Erreur MySQL : "'.$db.'" n\'existe pas.'); } public function prepareQuery($query) { for ($i=1; $i < func_num_args(); $i++) { $var = func_get_arg($i); if ( !is_scalar($var) && !is_null($var) ) throw new MyException('Erreur MySQL : impossible de préparer la reqête "'.$query.'"., problème d\'argument.'); if (is_string($var)) $var = mysql_real_escape_string($var); $query = str_replace('{'.$i.'}', $var, $query); } $this->query($query); } public function query($query) { $this->result = mysql_query($query); if (!$this->result) throw new MyException('Erreur MySQL : impossible d\'exécuter "'.$query.'".'); $this->query++; } public function getArray() { return mysql_fetch_array($this->result); } public function getRow() { return mysql_fetch_row($this->result); } public function getNumRows() { return mysql_num_rows($this->result); } public function close() { if (!mysql_close($this->id)) throw new MyException('Erreur MySQL : Impossible de fermer la connexion.'); unset($this->id); $this->bench->stop(); $this->bench = $this->bench->getResult(); } public function freeResult() { if (!mysql_free_result($this->result)) throw new MyException('Erreur MySQL : Impossible de libérer la mémoire.'); unset($this->result); }}?>