3.215.182.81 |    

Navigation

Google Advertisement

Class to connect to a mysql server and execute mysql statements. This class works with mysql COMMIT and ROLLBACK transaction.

mysql.class.php
  1. <?php
  2. /***************************************************************
  3.  * http://wiki.tuxnet24.de/
  4.  * Copyright (c) 2009 Marko Schulz <info@tuxnet24.de>
  5.  * All Rights Reserved.
  6.  *
  7.  * --LICENSE NOTICE--
  8.  * This program is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License
  10.  * as published by the Free Software Foundation; either version 2
  11.  * of the License, or (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  * --LICENSE NOTICE--
  22.  ***************************************************************
  23.  * $File: mysql.class.php $
  24.  * $Author: mschulz $ - $Date: 2009/01/11 22:27:08 $
  25.  * $Source: /var/lib/cvs/php-001/class/mysql.class.php,v $ - $Revision: 1.0 $
  26.  * $Description: Class to connect to mysql server and execute mysql statements. $
  27.  * $Copyright: (c) 2009 Marko Schulz - <info@tuxnet24.de> $
  28.  ***************************************************************/
  29. /**
  30.  * {{{ Sample
  31.  *
  32.  * <code>
  33.  *
  34.  * @include include/mysql.class.php
  35.  *
  36.  * (object) $sql = new Mysql( array( 'hostname' => $cfg['hostname'],
  37.  *					'username' => $cfg['username'],
  38.  *					'password' => $cfg['password'],
  39.  *					'database' => $cfg['database'] ) );
  40.  *
  41.  * (array) $query = array( 
  42.  *		"INSERT INTO `".$cfg['dbprefix']."user` SET `id` =NULL, `name` = 'fred'",
  43.  *		"INSERT INTO `".$cfg['dbprefix']."newuser` SET `id` =NULL, `name` = 'fred'"
  44.  * );
  45.  *
  46.  * if ( !$sql->query( $query ) ) echo "Can't insert new data set!";
  47.  * else echo "New data set was insered!";
  48.  *
  49.  * $sql->close();
  50.  *
  51.  * </code>
  52.  *
  53.  * }}}
  54.  */
  55. class Mysql {
  56.  
  57.  
  58. 	/**
  59. 	 * fatal error messages
  60. 	 * @var array
  61. 	 * @access private
  62. 	 */
  63. 	private static $ERROR = array(
  64. 		'EXTENSION' => "MySQL functions [%s] missing, you have to compile PHP with the --with-mysql option!",
  65. 		'CONNECTION' => "No connection can etablish to [%s] as mysql hostname!",
  66. 		'DATABASE' => "The database [%s] can't selected!"
  67. 	);
  68.  
  69.  
  70. 	/**
  71. 	 * This is the Class Constructor.
  72. 	 *
  73. 	 * @access public
  74. 	 * @static
  75. 	 * @param array $args
  76. 	 *  - string $args['hostname']
  77. 	 *  - string $args['username']
  78. 	 *  - string $args['password']
  79. 	 *  - string $args['database']
  80. 	 *  - bool $args['PERSISTENT']
  81. 	 * @return object
  82. 	 */
  83. 	public function __construct( array $args = array() ) {
  84.  
  85. 	if ( $args['hostname'] ) $this->open( $args );
  86.  
  87. 	}
  88.  
  89.  
  90. 	/**
  91. 	 * This method open the connection to the database server
  92. 	 * and select the database.
  93. 	 *
  94. 	 * @access private
  95. 	 * @static
  96. 	 * @param array &$args
  97. 	 *  - string $args['hostname']
  98. 	 *  - string $args['username']
  99. 	 *  - string $args['password']
  100. 	 *  - string $args['database']
  101. 	 *  - bool $args['PERSISTENT']
  102. 	 * @return object
  103. 	 */
  104. 	private function open ( array &$args = array() ) {
  105.  
  106. 	// Test for missing mysql.so, first try to load it
  107. 	if ( !@extension_loaded( 'mysql' ) ) @dl( 'mysql.so' );
  108.  
  109. 	// Fail now, otherwise we get a suppressed fatal error, which is very hard to track down
  110. 	if ( !function_exists( 'mysql_connect' ) ) $this->fatal( "EXTENSION", __LINE__, 'mysql_connect' );
  111.  
  112. 	// Get a connection to the mysql server.
  113. 	if ( $args['PERSISTENT'] === True )
  114. 		$this->connection = @/**/mysql_pconnect( $args['hostname'],
  115. 							$args['username'],
  116. 							$args['password'], True );
  117. 	else
  118. 		$this->connection = @/**/mysql_connect( $args['hostname'],
  119. 							$args['username'],
  120. 							$args['password'], True );
  121.  
  122. 	(bool) $success = False;
  123. 	if ( $args['database'] != '' ) {
  124. 		if ( $this->connection !== False )
  125. 			$success = @/**/mysql_select_db( $args['database'], $this->connection );
  126. 		else $success = False;
  127. 	} else $success = (bool) $this->connection; // Delay use query
  128.  
  129. 	if ( $success ) {
  130. 		(string) $version = $this->getServerVersion();
  131. 		if ( version_compare( $version, '4.1' ) >= 0 ) {
  132. 			// Tell the server we're communicating with it in UTF-8.
  133. 			// This may engage various charset conversions.
  134. 			$this->query( array( "SET NAMES utf8" ) );
  135. 			// Turn off strict mode
  136. 			$this->query( array( "SET sql_mode = ''" ) );
  137. 		}
  138. 	} else {
  139. 		if ( $this->connection !== False ) {
  140. 			$this->close();
  141. 			$this->fatal( "DATABASE", __LINE__, $args['database'] );
  142. 		} else $this->fatal( "CONNECTION", __LINE__, $args['hostname'] );
  143.  
  144. 	}
  145.  
  146. 	$this->opened = $success;
  147. 	return $this->opened;
  148.  
  149. 	}
  150.  
  151.  
  152. 	/**
  153. 	 * This method execute the mysql statement and return
  154. 	 * the result on SELECT or SHOW otherwise True/False.
  155. 	 *
  156. 	 * @access public
  157. 	 * @static
  158. 	 * @param array $query
  159. 	 * @return bool/string
  160. 	 */
  161. 	public function query ( $query ) {
  162.  
  163. 	(integer) $retval = 1;
  164.  
  165. 	// start transaction()
  166. 	$this->transaction();
  167.  
  168. 	// loop all entrys of the mysql statement
  169. 	foreach ( $query as $statement ) {
  170. 		(string) $statement = trim( $statement ); // remove whitespace character
  171. 		// return the result on SELECT or SHOW
  172. 		if ( preg_match( '/^(SELECT|SHOW)\s+/i', $statement ) )
  173. 			return mysql_query( $statement, $this->connection );
  174. 		else { // set $retval to 0 if one statement failed
  175. 			$result = mysql_query( $statement, $this->connection );
  176. 			if ( mysql_affected_rows() == 0 || $result == False ) $retval = 0;
  177. 		}
  178. 	}
  179.  
  180. 	// rollback the statement and return False
  181. 	if ( $retval == 0 ) {
  182. 		$this->rollback();
  183. 		return False;
  184. 	} else { // execute the statement and return True
  185. 		return $this->commit();
  186. 		return True;
  187. 	}
  188.  
  189. 	}
  190.  
  191.  
  192. 	/**
  193. 	 * This method close the mysql connection and return
  194. 	 * the True/False.
  195. 	 *
  196. 	 * @access public
  197. 	 * @static
  198. 	 * @return bool
  199. 	 */
  200. 	public function close() {
  201.  
  202. 	if ( $this->connection ) {
  203. 		if ( !@mysql_close( $this->connection ) ) return False;
  204. 		else return True;
  205. 	} else return True;
  206.  
  207. 	}
  208.  
  209.  
  210. 	/**
  211. 	 * This method return the last mysql error.
  212. 	 *
  213. 	 * @access public
  214. 	 * @static
  215. 	 * @return string
  216. 	 */
  217. 	public function error() {
  218.  
  219. 	if ( $this->connection ) {
  220. 		(string) $error = @/**/mysql_error( $this->connection );
  221. 		if ( !$error ) $error = mysql_error();
  222. 	} else (string) $error = mysql_error();
  223. 	return htmlspecialchars( $error );
  224.  
  225. 	}
  226.  
  227.  
  228. 	/**
  229. 	 * This method die's whith an fatal error.
  230. 	 *
  231. 	 * @access private
  232. 	 * @static
  233. 	 * @return void
  234. 	 */
  235. 	private function fatal( $name, $nr, $which = NULL ) {
  236.  
  237. 	if ( $which ) (string) $msg = str_replace( '%s', $which, Mysql::$ERROR[$name] );
  238. 	else (string) $msg = Mysql::$ERROR[$name];
  239. 	die( "MySQL Error in ".__FILE__." on line ".$nr.": ".$msg );
  240.  
  241. 	}
  242.  
  243.  
  244. 	/**
  245. 	 * This method start a mysql transaction.
  246. 	 *
  247. 	 * @access private
  248. 	 * @static
  249. 	 * @return bool
  250. 	 */
  251. 	private function transaction() {
  252. 		$null = mysql_query( "START TRANSACTION", $this->connection );
  253. 		return mysql_query( "BEGIN", $this->connection );
  254. 	}
  255.  
  256.  
  257. 	/**
  258. 	 * This method commit the last mysql transaction.
  259. 	 *
  260. 	 * @access private
  261. 	 * @static
  262. 	 * @return bool
  263. 	 */
  264. 	private function commit() {
  265. 		return mysql_query( "COMMIT", $this->connection );
  266. 	}
  267.  
  268.  
  269. 	/**
  270. 	 * This method rollback the last mysql transaction.
  271. 	 *
  272. 	 * @access private
  273. 	 * @static
  274. 	 * @return bool
  275. 	 */
  276. 	private function rollback() {
  277. 		return mysql_query( "ROLLBACK", $this->connection );
  278. 	}
  279.  
  280.  
  281. 	/**
  282. 	 * This method return the mysql server version.
  283. 	 *
  284. 	 * @access private
  285. 	 * @static
  286. 	 * @return string
  287. 	 */
  288. 	private function getServerVersion () {
  289. 		return mysql_get_server_info( $this->connection );
  290. 	}
  291.  
  292.  
  293. };
  294.  
  295. //**************************************************************
  296. // EOF
  297. ?>
Parsed in 0.024 seconds at 322.00 KB/s

Search
 
Full text search by name and content of a snippet.

User online
There are 2 users online.

Tags Cloud

Latest snippets
last day of last month
(Bash::Snippets)
crypt_apr1_md5
(PHP::Function)
crypt_apr1_md5
(Perl::Function)
transparent
(CSS)
rfc2822Toiso8601
(PHP::Function)
STDERR
(Bash::Function)
radius
(CSS)
shadow
(CSS)
rotate
(CSS)
create_function
(JavaScript::Snippets)