Location: PHPKode > projects > Kwalbum > kwalbum/include/DBConnection.php
<?php


/*
 * Created on Aug 7, 2008
 *
 */

require_once pathinfo(__FILE__, PATHINFO_DIRNAME).'/config.php';

class DBConnection
{
	public $db;

	public function DBConnection()
	{
		$this->db = @ new mysqli(MYSQL_HOST, MYSQL_USER_NAME, MYSQL_USER_PASSWORD, MYSQL_DB_NAME);

		/* check connection */
		if (mysqli_connect_errno())
		{
			echo "MySQL Connect Failed" . (TEST_MODE ? ': ' . mysqli_connect_error() : '!') . '<br>';
			exit ();
		}
	}

	/*
	 * $table: string: name of table to insert values into
	 * $data: array: key = field name, value = value
	 */
	public function Insert($table, $data)
	{
		$query = "INSERT INTO $table SET ";
		$useComma = false;
		foreach ($data as $field => $value)
		{
			if ($useComma)
				$query .= ', ';
			$query .= "$field='$value'";
			$useComma = true;
		}

		if (true === $this->db->query($query))
			return $this->db->insert_id;
		else
		{
			if (TEST_MODE)
			{
				global $DEBUG_INFO;
				$DEBUG_INFO .= "<p class='error'>".$this->db->error."<br/>$query</p>";
			}
			return false;
		}
	}

	public function UpdateSingle($table, $field, $value,  $condition)
	{
		return $this->Query("UPDATE $table SET $field=$value WHERE $condition");
	}

	public function UpdateMultiple($table, $data, $condition)
	{
		$query = "UPDATE $table SET ";
		$useComma = false;
		foreach ($data as $field => $value)
		{
			if ($useComma)
				$query .= ', ';
			$query .= "$field='$value'";
			$useComma = true;
		}
		return $this->Query($query." WHERE $condition");
	}

	public function GetCount($table, $condition = null)
	{
		$result = $this->Query("SELECT count(*) FROM $table".($condition ? " WHERE $condition" : null));
		if (0 == $result->num_rows)
			return 1;
		$row = $result->fetch_array();
		return $row[0];
	}

	public function Query($query)
	{
		$result = @$this->db->query($query);
		if (!$result and TEST_MODE)
		{
			global $DEBUG_INFO;
			$DEBUG_INFO .= "<p class='error'>".$this->db->error."<br/>$query</p>";
		}
		return $result;
	}

	public function Delete($table, $condition)
	{
		return $this->Query("DELETE FROM $table WHERE $condition");
	}

	// remove potentially dangerous code
	public function FilterString($input, $html=false, $length=0)
	{
		$input = strtr($input, array("<br/>" => "\n", "<br>" => "\n", "\r" => "", "&#039;" => "'"));
		if (!$html)
			$input=htmlspecialchars($input, ENT_QUOTES);
		if ($length > 0)
			$input=substr($input,0,$length);
		$input = strtr($input, array("\n" => "<br/>", "&amp;" => "&"));

		$blockedarray=array(
		'/config.php/i'
		);
		$input = preg_replace($blockedarray, '*blocked*', $input);

		if (get_magic_quotes_gpc())
			$input = stripslashes($input);
		if (!is_numeric($input))
			$input = $this->db->real_escape_string($input);
		return $input;
	}

	public function Close()
	{
		$this->db->close();
	}
}
?>
Return current item: Kwalbum