Location: PHPKode > projects > BuddyServ IRC Services > buddyserv1.5.1-release/extensions/buddyseed.1.0-geneva/class/Class.mysql.php
<?php

/***************************************************************************
 *   Copyright (C) 2009 by lynk   					                       *
 *   hide@address.com   						                           *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

/* Mysql Connections */

class mysql
{
	protected $object;
	protected $error_list = array();
	protected $valid = false;
	protected $persistent = true;
	public $hits = 0;

	/* Constructor only sets default persistent flag */
	public function __construct($persist=true)
	{
		$this->persistent = (($persist) ? true : false);
	}

	/* Magic getter */
	public function __get($what)
	{
		return $this->{$what};
	}

	/* Disconnect a sql connection */
	public function disconnect()
	{
		mysql_close($this->object);
	}

	/* Try to get a connection to the requested server, return false on fail, or true */
	public function connect($host, $port=3306, $password, $username, $database)
	{
		$this->object = mysql_connect($host, $username, $password, true);
		
		/* Check for valid resource */
		if (!$this->object)
		{
			return false;
		}

		else
		{
			if (!mysql_select_db($database, $this->object))
			{
				$this->valid = false;
				echo('Failed Connect:Unable to select database:'.mysql_error($this->object));
				return false;
			}

			$this->valid = true;
			return true;
		}
	}

	public function ping()
	{
		mysql_ping($this->object);
	}
	
	/* Perform a database query and return the query resource for future use */
	public function query($query_string)
	{
		global $me, $irc;
		
		$this->hits++;
		
		$query = mysql_query($query_string, $this->object);

		/* Check for sql errors */
		if ($error = mysql_error($this->object))
		{
			echo('Failed Connection: ' . $error);
			echo($query_string);
			
		//	interm::send("\0034\2!Severe!\003\2: SQL failure: ");
			
			if ($error == 'MySQL server has gone away')
			{
				echo 'CONNECT '.SQL_HOST .' - '. SQL_PASS .' - '.SQL_USER.' - '.SQL_DB;
				if ($this->connect(SQL_HOST, '3306', SQL_PASS, SQL_USER, SQL_DB))
					$this->query($query_string);
					
				else 
					new error(false, 'Unable to reconnect to sql');
			}
			
			return false;
		}

		return $query;
	}

	/* Retrieves the number of results from a query */
	public function num_rows($query)
	{
		if (!$query)
			return;
			
		return mysql_num_rows($query);
	}

	/* Retrieves number of affected rows of a query */
	public function num_affected($query)
	{
		return mysql_affected_rows($this->object);
	}
	
	public function insert_id($query=false)
	{
		return mysql_insert_id($this->object);
	}

	/* Insert a record into the given table. Data is stored in an associative array for the insert
	 * Returns record ID or false */
	public function insert($table, $array, $sanitize=true)
	{
		/* Check if array is empty */
		if (count($array) < 1)
		{
			$this->_error('Failed Insert: Name/Value array contains no values');
			return false;
		}

		/* Sanitize and create the beginning of the insert string */
		$table = (($sanitize) ? mysql_real_escape_string($table) : $table);
		$string = "INSERT INTO `".$table."` (";

		/* We store the values in an array to add to the string later */
		$value_array = array();
		$count = 0; /* just used to add preceding commas before each entry */

		foreach ($array as $name => $value)
		{
			if ($count > 0)
				$string .= ', ';

			/* add the name to the insert field area */
			$string .= '`' . (($sanitize) ? mysql_real_escape_string($name) : $name) .'`';

			/* We create a standard array with the values in order for later appending */
			$value_array[] = $value;
			$count++;
		}

		/* Start the values code */
		$string .= ') VALUES (';

		$count = 0;

		foreach ($value_array as $value)
		{
			if ($count > 0)
				$string .= ', ';

			/* Add the values to the string in the proper order */
			$string .= '\''. (($sanitize) ? mysql_real_escape_string($value) : $value) . '\'';
			$count++;
		}

		$string .= ')';

		/* Run the query */
		$query = mysql_query($string, $this->object);

		/* If the insert id is false or 0 it means the query failed */
		if (!mysql_insert_id($query))
		{
			$this->_error('Failed Insert: '. mysql_error($query));
			return false;
		}

		else
		{
			/* Return the new row ID */
			return mysql_insert_id($query);
		}
	}

	/* Update function on a row or multiple thereof */
	public function update($table,$array,$where,$limit,$sanitize=true)
	{
		/* Ensure valid array */
		if (count($array) < 1)
		{
			$this->_error('Failed Update: Name/Value array contains no entries');
			return false;
		}

		$string = 'UPDATE `'. (($sanitize) ? mysql_real_escape_string($table) : $table) .'` SET ';
		$count = 0;

		/* Cycle the array of name/values that need to be changing */
		foreach ($array as $name => $value)
		{
			if ($count > 0)
				$string .= ', ';

			$string .= 'SET `'. (($sanitize) ? mysql_real_escape_string($name) : $name) .'` = \''. (($sanitize) ? mysql_real_escape_string($value) : $value) .'\'';
			$count++;
		}

		/* Add raw WHERE clause if available */
		if ($where)
		{
			$string .= ' WHERE '. $where .' ';
		}

		/* If the limit is a valid integer add the LIMIT keyword */
		if (is_int($limit))
			$string .= ' LIMIT '. $limit;

		$query = mysql_query($string, $this->object);

		/* Check for sql error */
		if ($error = mysql_error($query))
		{
			$this->_error('Failed Update: '. $error);
			return false;
		}

		else
		{
			return mysql_affected_rows($query);
		}
	}

	public function fetch_array(&$query)
	{
		return mysql_fetch_array($query);
	}

	public function fetch_row(&$query)
	{
		return mysql_fetch_row($query);
	}

	public function fetch_object(&$query)
	{
		return mysql_fetch_object($query);
	}

	protected function _error($message)
	{
		$this->error_list[] = $message;
	}

	public function escape($zomgevilzombiestring)
	{
		return mysql_real_escape_string($zomgevilzombiestring, $this->object);
	}
	
	public function last_errors()
	{
		if (!count($this->error_list))
			return false;

		else
		{
			$array = $this->error_list;
			$this->error_list = array();

			return $array;
		}
	}

		/* Deletes a row or multiple rows from the database */
	public function delete($table, $where, $limit=false, $sanitize=true)
	{
		if (!$where)
		{
			$this->_error('Failed Delete: Missing WHERE clause.');
			return false;
		}

		$string = 'DELETE FROM `'. (($sanitize) ? mysql_real_escape_string($table) : $table) . 'WHERE '. (($sanitize) ? mysql_real_escape_string($where) : $where);

		if (is_int($limit))
			$string .= ' LIMIT '. $limit;

		$query = mysql_query($string, $this->object);

		if ($error = mysql_error($query))
		{
			$this->_error('Failed Delete: '. $error);
			return false;
		}

		/* return the affected rows count */
		return mysql_affected_rows($query);
	}

}

?>
Return current item: BuddyServ IRC Services