PHP Classes

File: mysql.inc.php

Recommend this page to a friend!
  Classes of Fanache A. Remus   SQL class for PHP   mysql.inc.php   Download  
File: mysql.inc.php
Role: Class source
Content type: text/plain
Description: MySQL class
Class: SQL class for PHP
Access different databases with the same interface
Author: By
Last change: - rename sql class from SQL_DB to sqltype_DB (where sqltype is MYSQL or MYSQLI or SQLITE2 or SQLITE3 or MSSQL)
(this is required if you want to use 2 different sql databases)
(Note: after update to this revision you need to initialize class with specific name, see examples)
- corrected a bug on mysql/mysqli class (bug sql port if is different from 3306)
- add $this->sql_query_monitor (use to monitor query)
(Note: before this revision if you use $this->sql_tracer you get all log from class, include filters and other data and now with $this->sql_query_monitor you can see now only executed queries)
- corrected a bug on all classes on log
(before this update if is to many queries you get an memory error, and now you can avoid this problem by using $this-sql_debug = false;)
- add $this->query_log() (protected function - use $this->sql_query_log instead)
- corrected a bug on $this->real_escape() function
- corrected a bug on $this->free_result() function
- add example_2_db.php (example for using 2 databases of same type)
- add example_from_mysqli_to_sqlite3.php (example for 2 different databases)
- update example.php
- added mysqli class (for php 5)
- rewrite mysql.inc.php for php 5 standards
- added new mysql and mysqli examples (create table, select, insert, delete, drop)
- added new function for mysql::set_time_zone
- code review (set all messages and comments on english language)
- bug fixed on MSSQL::real_escape
- added new function array_sqlfilter() (improvement)
- change function name from real_escape_string to real_escape
- change function name from sql_prepare_data to prepare_data
- corect bug on real_escape function
- corect bug on prepare_data function
Date: 9 years ago
Size: 15,782 bytes
 

Contents

Class file image Download
<?php /** * @Package: FAR-PHP CMS * @Author: Fanache A. Remus (Birkoff) * @$Date$ * @Contact: www.farsoft.far-php.ro - contact@far-php.ro * @$Rev$ * @$Id$ * * @Desc: MySQL class for php 5.0.x - 5.4.x * * @Note: * - for older version (< 5.x) please use older version of this class * - you can find older version on repository on http://sqlclassphp.sourceforge.net/ * - for php version > 5.5.x please use mysqli.inc.php or PDO version **/ // This file can not be accessed independently from index if(!defined('FAR_ANTIHACK')) { header('HTTP/1.0 403 Forbidden'); header('Status: 403 Forbidden'); die("<br>You don't have access here."); } class MYSQL_DB { // initializing the internal variables // monitoring data public $sql_tracer = array(); // if true provides information on every function called, otherwise only provides information of queries public $sql_debug = true; public $sql_query_monitor=true; // connecting public $conn = NULL; // variable that holds the results returned public $result = false; // db info protected $db_server = '127.0.0.1'; protected $db_port = '3306'; protected $db_username = 'root'; protected $db_password = ''; protected $db_name = ''; protected $db_encoding = 'utf8'; protected $db_time_zone = 'Europe/Bucharest'; protected $db_persist = false; /** * class instance * * @param str $db_server - sql server name * @param int $db_port - sql server connection port * @param str $db_username - sql username * @param str $db_password - sql password * @param str $db_name - database name * @param str $db_encoding - encoding data type * @param bool $db_persist - connection type * **/ public function __construct ($db_server='', $db_port='', $db_username='', $db_password='', $db_name='', $db_encoding='', $db_persist=false) { if ( ! empty($db_server) ) $this->db_server = $db_server; if ( ! empty($db_port) && is_numeric($db_port) ) $this->db_port = $db_port; if ( ! empty($db_username) ) $this->db_username = $db_username; if ( ! empty($db_password) ) $this->db_password = $db_password; if ( ! empty($db_name) ) $this->db_name = $db_name; if ( ! empty($db_encoding) ) $this->db_encoding = $db_encoding; if ( $db_persist === true ) $this->db_persist = $db_persist; return $this->open(); } /** * connecting to sql server * database selection * encoding type setting * * @return resource $conn - false on failure * **/ protected function open() { if ( $this->connect() === false ) return false; if ( $this->select_db() === false ) return false; if ( $this->set_charset() === false ) return false; return $this->conn; } /** * connecting to the database * * @param str $db_server * @param int $db_port * @param str $db_username * @param str $db_password * * @return bool * **/ protected function connect($db_server='', $db_port='', $db_username='', $db_password='') { if ( ! empty($db_server) ) $this->db_server = $db_server; if ( ! empty($db_port) && is_numeric($db_port) ) $this->db_port = $db_port; if ( ! empty($db_username) ) $this->db_username = $db_username; if ( ! empty($db_password) ) $this->db_password = $db_password; // if it is already connected if( is_resource($this->conn) ) return true; // Choose the appropriate connect function if ( $this->db_persist ) $this->conn = @mysql_pconnect($this->db_server.':'.$this->db_port, $this->db_username, $this->db_password); else $this->conn = @mysql_connect($this->db_server.':'.$this->db_port, $this->db_username, $this->db_password); // Connect to the database server if(!is_resource($this->conn)) { if ( $this->sql_debug ) $this->sql_monitor('connect', 'connect', mysql_error(), $this->conn, __LINE__, __FILE__, mysql_errno(), mysql_error()); return false; } else { if ( $this->sql_debug ) $this->sql_monitor('connect', 'connect', mysql_error(), $this->conn, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error()); return true; } } /** * database selection * * @param str $db_name * * @return bool * **/ public function select_db($db_name='') { if( empty($db_name) ) $db_name = $this->db_name; $rez = mysql_select_db($db_name,$this->conn); if ( $this->sql_debug ) $this->sql_monitor('select_db', 'db_name', $db_name, $rez, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error()); return $rez; } /** * charset setting used * * @param str $db_encoding * * @return bool * **/ public function set_charset($db_encoding='') { if ( empty($db_encoding) ) $db_encoding = $this->db_encoding; $rez = mysql_set_charset($db_encoding,$this->conn); if ( $this->sql_debug ) $this->sql_monitor('set_charset', 'db_encoding', $db_encoding, $rez, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error()); return $rez; } /** * set time zone * * @param str $time_zone * * @return bool */ public function set_time_zone($time_zone='') { if ( empty($time_zone) ) $db_time_zone = $this->db_time_zone; else $db_time_zone = $time_zone; $rez = $this->query("SET time_zone = '$db_time_zone'",$this->conn); if ( $this->sql_debug ) $this->sql_monitor('set_time_zone', 'time_zone', $time_zone, $rez, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error()); return $rez; } /** * monitoring data and errors * * @param str $function_name - function name that is monitoring * @param str $param_name - function parameter name * @param obj $param_val - function parameter value * @param obj $return_val - value returned * @param int $line - line number where you can find values * @param str $file - file name * @param str $error - reported error * **/ protected function sql_monitor($function_name, $param_name, $param_val, $return_val, $line, $file, $error_nr=0, $error_val=0) { $this->sql_tracer[] = array( 'function_name' => $function_name, 'param_name' => $param_name, 'param_val' => $param_val, 'return_val' => $return_val, 'line' => $line, 'file' => $file, 'error_nr' => $error_nr, 'error_val' => $error_val, ); } /** * monitoring queryes * * @param str $function_name - function name that is monitoring * @param str $param_name - function parameter name * @param obj $param_val - function parameter value * @param obj $return_val - value returned * @param int $line - line number where you can find values * @param str $file - file name * @param str $error - reported error * **/ protected function query_log($function_name, $param_name, $param_val, $return_val, $line, $file, $error_nr=0, $error_val=0) { $this->sql_query_log[] = array( 'function_name' => $function_name, 'param_name' => $param_name, 'param_val' => $param_val, 'return_val' => $return_val, 'line' => $line, 'file' => $file, 'error_nr' => $error_nr, 'error_val' => $error_val, ); } /** * identification number of the current error - mysql_errno * * @return int $nr * **/ public function sql_errno() { if ( ! is_resource($this->conn) ) return 0; else return mysql_errno($this->conn); } /** * last error reported - mysql_error * * @return str $error * **/ public function sql_error() { if ( ! is_resource($this->conn) ) return 0; else return mysql_error($this->conn); } /** * making query - mysql_query * * @param str $query * * @return resource $result * **/ public function query($query) { if ( empty($query) ) return false; $this->result = mysql_query($query,$this->conn); if ( $this->sql_debug ) $this->sql_monitor('query', 'query', $query, $this->result, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error()); if ( $this->sql_query_monitor ) $this->query_log('query', 'query', $query, $this->result, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error()); return $this->result; } /** * parsing the data as array - mysql_fetch_array * * @param resource $rezultat * @param bool $type - true for MYSQL_ASSOC, false for MYSQL_NUM, null for MYSQL_BOTH * * @return array $result * **/ public function fetch_array($rezultat=NULL, $type=true) { if ( is_resource($rezultat) ) $result = $rezultat; else $result = $this->result; if ( $type == true) $rez = mysql_fetch_array($result, MYSQL_ASSOC); elseif ( $type == false) $rez = mysql_fetch_array($result, MYSQL_NUM); else $rez = mysql_fetch_array($result, MYSQL_BOTH); if ( $this->sql_debug ) $this->sql_monitor('fetch_array', 'result', $result, $rez, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error()); return $rez; } /** * parsing the data as array - mysql_fetch_array * * @param resource $rezultat - The result resource that is being evaluated. This result comes from a call to mysql_query(). * @param str $class_name - The name of the class to instantiate, set the properties of and return. If not specified, a stdClass object is returned * @param array $params - An optional array of parameters to pass to the constructor for class_name objects. * * @return array $result * **/ public function fetch_object($rezultat=NULL, $class_name=NULL, $params=NULL) { if ( is_resource($rezultat) ) $result = $rezultat; else $result = $this->result; if (is_string($class_name) && ! empty($class_name)) { if (is_array($params) && count($params)) { $rez = mysql_fetch_object($result, $class_name, $params); } else { $rez = mysql_fetch_object($result, $class_name); } } else { $rez = mysql_fetch_object($result); } if ( $this->sql_debug ) $this->sql_monitor('fetch_object', 'result', $result, $rez, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error()); return $rez; } /** * closing connection - mysql_close * * @return bool $rez * **/ public function close() { if (is_resource($this->conn)) $rez = mysql_close($this->conn); else $rez = $this->conn; if ( $this->sql_debug ) $this->sql_monitor('close', '', '', $rez, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error()); return $rez; } /** * destructor of this class - for now, only close sql connection * * @return bool */ public function __destruct() { return $this->close(); } /** * start tranzaction * * @return resource * **/ public function start_tranzaction() { return $this->query('START TRANSACTION'); } /** * rollback tranzaction * * @return resource * **/ public function rollback() { return $this->query('ROLLBACK'); } /** * commit tranzaction * * @return resource * **/ public function commit() { return $this->query('COMMIT'); } /** * number of rows affected - mysql_affected_rows * * @return int $rows * **/ public function affected_rows() { $rows = mysql_affected_rows($this->conn); if ( $this->sql_debug ) $this->sql_monitor('affected_rows', '', '', $rows, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error()); return $rows; } /** * inserted row id - mysql_insert_id * * @return int $id * **/ public function insert_id() { $id = mysql_insert_id($this->conn); if ( $this->sql_debug ) $this->sql_monitor('insert_id', '', '', $id, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error()); return $id; } /** * filter values - mysql_real_escape_string * * @param mixed $str * * @return mixed $str * **/ public function real_escape($str) { if ( is_array($str) ) $rez = array_map('mysql_real_escape_string', $str); else $rez = mysql_real_escape_string($str, $this->conn); if ( $this->sql_debug ) $this->sql_monitor('real_escape', 'str', $str, $rez, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error()); return $rez; } /** * number of rows found - mysql_num_rows * * @param resource $rezultat * * @return int $result * **/ public function num_rows($rezultat=NULL) { if ( ! is_resource($rezultat) ) $rez = $this->result; else $rez = $rezultat; $result = mysql_num_rows($rez); if ( $this->sql_debug ) $this->sql_monitor('num_rows', 'rez', $rez, $result, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error()); return $result; } /** * result returned by the query - mysql_result * * @param resource $rezultat * @param int $row * * @return str $result * **/ public function result($rezultat=NULL, $row=0) { if ( ! is_resource($rezultat) ) $rez = $this->result; else $rez = $rezultat; $result = mysql_result($rez, $row); if ( $this->sql_debug ) $this->sql_monitor('result', 'rez', $rez, $result, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error()); return $result; } /** * information about SQL - mysql_info * * @return str $info * **/ public function sql_info() { $info = mysql_info($this->conn); if ( $this->sql_debug ) $this->sql_monitor('sql_info', '', '', $info, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error()); return $info; } /** * buffer empty sql - mysql_free_result * * @param resource $rez * * @return bool * **/ public function free_result($rezultat=NULL) { if ( ! is_resource($rezultat) ) $rez = $this->result; else $rez = $rezultat; $info = mysql_free_result($rez); if ( $this->sql_debug ) $this->sql_monitor('free_result', '', '', $info, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error()); return $info; } /** * Preparation for working with MySQL data * * @param array $values - values that need to be changed * @param bool $tilde - what type of quotes used * * @return string $values * **/ public static function prepare_data($values, $tilde = false) { if (! is_array($values) || count($values) == 0) return false; if ($tilde) $values = count($values) ? "`" . implode("`, `", $values) . "`" : ''; else $values = count($values) ? "'" . implode("', '", $values) . "'" : ''; return $values; } /** * Filter an array by keys * * @param array $array associative array to filter * @param array $filter list of keys to return (in this order) * * @return array a new array containing only those keys and values from $array which exists in $filter too (keep order from $filter) * **/ public static function array_sqlfilter($array, $filter) { if (! is_array($array) || count($array) == 0) return false; if (! is_array($filter) || count($filter) == 0) return false; $result = array(); foreach ($filter as $key) { if (isset($array[$key])) $result[$key] = $array[$key]; } return $result; } /** * return server version * */ public function get_server_info() { return mysql_get_server_info($this->conn); } /** * return client sql version */ public function get_client_info() { return mysql_get_client_info(); } } ?>