<?php
/*
 *    Copyright (c) 2010 VidiScript
 *
 *    This file is part of VidiScript.
 *
 *    VidiScript 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 3 of the License, or
 *    (at your option) any later version.
 *
 *    VidiScript 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 VidiScript.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    File Name: mysql.inc
 *    Description:  Used throughout the script to handle MySQL connections and queries
 *    $Date: 2010-02-21 23:16:57 +0000 (Sun, 21 Feb 2010) $
 *    $Revision: 12 $
 */
class mysql {
	var $conId ;
	var $lastId ;
	var $result ;
	function mysql($host, $user, $password, $database) {
		$this->conId = @mysql_connect($host, $user, $password) or $this->_error(mysql_error()) ;
		@mysql_select_db($database) or $this->_error(mysql_error()) ;
	}
	function query($query) {
		$this->result = mysql_query($query, $this->conId) ;
		$this->lastId = mysql_insert_id($this->conId) ;
		if ($this->result == false) {
			$this->_queryError(mysql_error(), $query) ;
		}
		else {
			return true ;
		}
	}
	function numRows() {
		return mysql_num_rows($this->result) ;
	}
	function fetch($mode = MYSQL_ASSOC ) {
		return mysql_fetch_array($this->result, $mode) ;
	}
	function fetchAll($mode = MYSQL_ASSOC ) {
		$arr = array() ;
		while ($row = $this->fetch($mode)) {
			$arr[] = $row ;
		}
		return $arr ;
	}
	function getLastId() {
		return $this->lastId ;
	}
	function freeResult() {
		return mysql_free_result($this->result) ;
	}
	function disconnect() {
		$ret = mysql_close($this->conId) ;
		$this->conId = NULL ;
		return $ret ;
	}
	function _error($error) {
		die($error) ;
	}
	function _queryError($error, $query) {
		die('<p><pre>Query error:<br/>'.htmlspecialchars($query).'</pre></p><p>'.$error.'</p>') ;
	}
}
?>
