Knihovna php mysql
Z Varhoo
Jednoduchá knihovna na zpracování dotazů mysql obsažená ve frameworku PHP/Vlašský ořech
<?php global $__mysql; class db extends mysql { }
class mysql { var $error = array(); var $insertId = NULL; private $link;
function __construct() { global $__mysql, $settings; if($__mysql == NULL) { $__mysql = mysql_connect($settings["DB_HOST"],$settings["DB_USER"],$settings["DB_PASS"]); if(!$__mysql) die("{$settings["DB_HOST"]} not found"); mysql_query("SET CHARACTER SET utf8",$__mysql); mysql_query("SET NAMES utf8",$__mysql); mysql_select_db($settings["DB_NAME"],$__mysql); } $this->link = $__mysql; $this->functionArray = array("NOW()"); }
function insert($table,$data) { if(!is_array($data) || empty($table)) { return "NOT_DATA"; } foreach($data as $key => $col) { if(in_array($col,$this->functionArray)) $set[] = "`{$key}`={$col}" ; else { $col = mysql_real_escape_string($col); $set[] = "`{$key}`='{$col}'" ; } } $query = "INSERT INTO `{$table}` SET ".implode(", ",$set); $this->query($query); $this->insertId = mysql_insert_id(); return $this->insertId; }
function query($query) { $this->query[]=$query; $result = mysql_query($query,$this->link); $this->lastInsertId = mysql_insert_id(); $error = mysql_error(); if($error!=NULL) { $this->error[] = $error; $this->log(); return false; } return $result; }
function log(){ echo "MySQL Error: ".end($this->error)."\n"; }
function lastInsertId() { return $this->lastInsertId; }
function update($table, $data, $where = NULL,$limit=1) { foreach($data as $key => $col) { if(in_array($col,$this->functionArray)) $set[] = "`{$key}`={$col}" ; else { $col = mysql_real_escape_string($col); $set[] = "`{$key}`='{$col}'" ; } } if($where != NULL){ $sql = "UPDATE `{$table}` SET ".implode(", ",$set)." WHERE {$where} LIMIT {$limit}"; } else { $sql = "UPDATE `{$table}` SET ".implode(", ",$set)." "; } return $this->query($sql); }
function queryArray($query, $number = NULL) { $result = $this->query($query); if($result) { while($row = mysql_fetch_object($result)) { $data[] = array_map("stripslashes",(array) $row); } } if($number!=NULL) return $data[$number-1]; else return $data; }
function queryObject($query, $number = NULL) { $result = $this->query($query); $data = array(); if($result) { while($row = mysql_fetch_object($result)) { $data[] = array_map("stripslashes",$row); } } if($number!=NULL) return $data[$number-1]; else return $data; }
/* ziskání všech řádků z databáze */
function fetchAll($query) { return $this->queryArray($query); }
/* získání prvního řádku z databáze */
function fetchOne($query) { return $this->queryArray($query, 1); } } ?>