Location: PHPKode > projects > Echelon - A Writing Center Portal > echelon-1.0.0/include/sql_manip.php
<?php
/* sql_manip.php -- Defines the SQL_manip class */

// This version is written to layer on top of MDB2 to streamline direct SQL execution
// Compatible with PHP 5+

require_once("config.php");
require_once("MDB2.php");

class SQL_manip
{
	private $dsn;
	private $mdb2_obj;
	private $current_database;
	private $result_obj;

	function __construct($host = null, $username = null, $password = null, $database = null)
	{
		if (func_num_args() != 0)
		{
			$this->dsn = $this->make_dns_withargs($host, $username, $password, $database);
		}
		else
		{
			$this->dsn = $this->make_dns_noargs();
		}

		$this->mdb2_obj = &MDB2::connect($this->dsn);
		if (PEAR::isError($this->mdb2_obj))
		{
			die('Cannot connect: ' . $this->mdb2_obj->getMessage());
		}
	}

	function make_dns_withargs($host, $username, $password, $database)
	{
		$dsn_arr = array (
				'phptype'  => 'mysql',
				'username' => $username,
				'password' => $password,
				'hostspec' => $host,
				'database' => $database,
			);
		$this->current_database = $database;
		return $dsn_arr;
	}

	function make_dns_noargs()
	{
		global $DATABASE;

		$dsn_arr = array (
				'phptype'  => 'mysql',
				'username' => $DATABASE['user'],
				'password' => $DATABASE['password'],
				'hostspec' => $DATABASE['host'],
				'database' => $DATABASE['name'],
			);
		$this->current_database = $DATABASE['name'];

		return $dsn_arr;
	}

	function get_dsn()
	{
		return $this->dsn;
	}

	function get_database_name()
	{
		return $this->current_database;
	}

	function set_db($dbname)
	{
		$mdb2 = $this->mdb2_obj;
		$mdb2->setDatabase($dbname);
		$this->current_database = $dbname;
	}

	function exec($query_str)
	{
		$mdb2 = $this->mdb2_obj;
		$res = $mdb2->exec($query_str);

		if (PEAR::isError($res))
		{
			die('Cannot exec(): ' . $res->getMessage());
		}
	}

	function query($query_str)
	{
		$mdb2 = $this->mdb2_obj;
		$res = $mdb2->query($query_str);

		if (PEAR::isError($res))
		{
			die('Cannot query(): ' . $res->getMessage());
		}
		$this->result_obj = $res;
	}

	function fetch_assoc($q = null, $newquery = true)
	{
		if (func_num_args() != 0)
		{
			($newquery) ? $this->query($q) : null;
		}
		$res = $this->result_obj;
		return $res->fetchRow(MDB2_FETCHMODE_ASSOC);
	}

	function fetch_object($q = null, $newquery = true)
	{
		if (func_num_args() != 0)
		{
			($newquery) ? $this->query($q) : null;
		}
		$res = $this->result_obj;
		return $res->fetchRow(MDB2_FETCHMODE_OBJECT);
	}

	function fetch_num($q = null, $newquery = true)
	{
		if (func_num_args() != 0)
		{
			($newquery) ? $this->query($q) : null;
		}
		$res = $this->result_obj;
		return $res->fetchRow(MDB2_FETCHMODE_ORDERED);
	}

	function get_num_rows()
	{
		$res = $this->result_obj;
		return $res->numRows();
	}

	function close()
	{
		$mdb2 = $this->mdb2_obj;
		$mdb2->disconnect();
	}

	function __destruct()
	{
		$this->close();
	}

	function get_fields($fields_str, $table, $where = "")
	{
		$where = ($where != "") ? "WHERE " . $where : $where;
		$query_str = "SELECT " . $fields_str . " FROM " . $table . " " . $where;
		// echo $query_str;
		$this->query($query_str);
	}
}
?>
Return current item: Echelon - A Writing Center Portal