Location: PHPKode > projects > Katyshop > katyshop_ro_0.3.2.1/classes/common/MysqlDatabase.php
<?php

/**
 * Product: Katyshop
 * @version 0.3.2.1
 * @author Catalin Hulea - hide@address.com
 * @copyright Copyright (C) 2007 Catalin Hulea
 * @license GNU General Public License version 3
 * 			You can find a copy of GNU GPL v3 at this path: /docs/LICENSE
 * @link https://sourceforge.net/projects/katyshop
 * 
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 * 
 */

require_once(dirname(__FILE__) . "/BaseObject.php");

class MysqlDatabase extends BaseObject
{
	var $con;
	var $host;
	var $user;
	var $pass;
	var $name;

	/**
	 * Constructor
	 *
	 * @param string $dbHost
	 * @param string $username
	 * @param string $password
	 * @param string $dbName
	 * @return MysqlDatabase
	 */
	function MysqlDatabase($dbHost, $username, $password, $dbName)
	{
		$this->host = $dbHost;
		$this->user = $username;
		$this->pass = $password;
		$this->name = $dbName;
	}

	/**
	 * Try to open connection. Returns true on success or false on failure.
	 *
	 * @return boolean
	 */
	function open()
	{
		$this->con = mysql_connect($this->host, $this->user, $this->pass);
		if(!$this->con)
			return false;
		elseif(!mysql_select_db($this->name, $this->con))
			return false;
		else
			return true;
	}

	/**
	 * Close connection to database.
	 *
	 * @return boolean
	 */
	function close()
	{
		return mysql_close($this->con);
	}

	/**
	 * Escape a string parameter, in order to prepare it
	 * for sql query (avoid injection)
	 *
	 * @param string $str
	 * @return string
	 */
	function escape($str)
	{
		return mysql_escape_string($str);
	}

	/**
	 * Send a mysql query AS-IS written in $q parameter.
	 * Nothing is escaped by this function, just send the query.
	 * Example: $result = $db->query("SELECT * FROM users");
	 *
	 * @param string $q
	 */
	function query($q)
	{
		mysql_select_db($this->name, $this->con);
		$res = mysql_query($q, $this->con);
		if($res === false)
		{
			Logger::err("Database::query() failed with error message: " . mysql_error(), __FILE__, __LINE__);
			Logger::err("Query was: $q", __FILE__, __LINE__);
			die(mysql_error());
		}
		return $res;
	}

	/**
	 * @return int
	 */
	function lastInsertId()
	{
		return mysql_insert_id($this->con);
	}

	function fetch_array($res)
	{
		return mysql_fetch_array($res);
	}
}
?>
Return current item: Katyshop