Location: PHPKode > projects > Modular Site Manager > htdocs/inc/mysql.inc.php
<?php
/*
 * MySQL Class
 * @Copyright: Vouksh.info 2005
 * @Description: A class to connect to a mysql database, and some stuff like select, delete, update, ect
 */

#TODO: Optimize and secure it. Possibly cache it, but I dunno.
class mysql
{

	var $connected = NULL;
	var $mdc = NULL;
	var $qnum = 0;

	//Create the connector!
	function mysql($db, $nc=false)
	{
		global $REMOTE_ADDR;
		if(!$nc)
		{
			$this->mdc = mysql_connect("localhost", MYSQL_USER, MYSQL_PASS) or $this->error();
			mysql_select_db($db, $this->mdc);
			$this->connected = true;
		}
		return true;
	}

	function close()
	{
		// Check to see if we're connected first..
		if($this->connected !== true)
		{
			// If we're not connected, return false!
			$ret = false;
			echo "You must connect before you close retard!";
		} else {
			// W00t, we're smart and connected first!
			mysql_close($this->mdc) or $this->error();
			$ret = true;
			echo '<!-- queries: '. $this->qnum .' -->';

		}
		// Do we get and A or an F?
		return $ret;
	}

	// Hmm.... We select from a table here.....
	function select($table, $colums, $ord, $sort = NULL, $limit = NULL)
	{

		if(!is_null($colums))
		{
			$query .= ' WHERE '.$colums;
		}
		$query .= ' ORDER BY '.$ord;
		if(!is_null($sort))
		{
			$query .= ' '.$sort;
		}
		if(!is_null($limit))
		{
			$query .= ' LIMIT '. $limit;
		}
		$ret = @mysql_query('SELECT * FROM '.$table.$query) or $this->error();
		$this->qnum++;
		return $ret;
	}

	function update($table, $data, $where)
	{
		// Again, check our connection
		if($this->connected !== true)
		{
			// Ohhhh, you gots an F!
			$ret = false;
			echo "You gotta connect before you can update anything bud!";
		} else {
			$ret = @mysql_query("UPDATE ".$table." SET ".$data." WHERE ".$where." LIMIT 1", $this->mdc) or $this->error();
		}
		$this->qnum++;
		return $ret;
	}

	function delete($table, $data, $limit="1")
	{
		// Again, check our connection
		if($this->connected !== true)
		{
			// Ohhhh, you gots an F!
			$ret = false;
			echo "You gotta connect before you can delete anything bud!";
		} else {
			$ret = @mysql_query("DELETE FROM ".$table." WHERE ".$data." LIMIT ".$limit, $this->mdc) or $this-error();
		}
		$this->qnum++;
		return $ret;
	}

	function query($query)
	{
		if($this->connected !== true)
		{
			// Ohhhh, you gots an F!
			$ret = false;
			echo "You gotta connect before you can delete anything bud!";
		} else {
			$ret = @mysql_query($query, $this->mdc) or $this->error();
		}
		$this->qnum++;
		return $ret;
	}

	//Slightly advanced insert function. pretty easy to use, not too hard to code either ;)
	function insert($table, $cols, $vals)
	{
		$ret = "INSERT INTO ".$table." ( ";
		// check to see if the columns is an array or not..
		if(is_array($cols))
		{
			// it is! so lets loop through them, and add them to the query
			$t = count($cols);
			$i = 0;
			foreach($cols as $col)
			{
				$i++;
				//checking to see if it's the last one in the array.
				if($i !== $t)
				{
					$ret .= "`".$col."` , ";
				} else {
					$ret .= "`".$col."` ";
				}
			}
		} else {
			//it's not an arry.. so lets just slap it in there
			$ret .= "`".$cols."`";
		}
		$ret .= " ) VALUES (";
		//same thing as above, but with the insert values
		if(is_array($vals))
		{
			$t = count($vals);
			$i = 0;
			foreach($vals as $val)
			{
				$i++;
				if($i !== $t)
				{
					$ret .= "'".$val."', ";
				} else {
					$ret .= "'".$val."'";
				}
			}
		} else {
			$ret .= "'".$vals."'";
		}
		$ret .= ")";
		$this->qnum++;
		return @mysql_query($ret) or $this->error();
	}

	function rcount($table, $where=NULL, $limit=-1)
	{
		$query = "SELECT * FROM ".$table;
		if(!is_null($where))
		{
			$query .= " WHERE ".$where;
		}
		if($limit > 0)
		{
			$query .= " LIMIT ".$limit;
		}
		$ret = @mysql_num_rows(mysql_query($query)) or $this->error();
		return($ret);
	}

	function error()
	{
		global $REMOTE_ADDR, $_SERVER;
		$fp = fopen(SITE_PATH."logs/error.mysql.log", "a");
		fwrite($fp, $REMOTE_ADDR." ERROR: ".mysql_error()."; URL: ".$_SERVER['REQUEST_URI']."\n");
		mail("hide@address.com", "MySQL Error", $REMOTE_ADDR." ERROR: ".mysql_error()."\n\nURL: ".$_SERVER['REQUEST_URI'], "FROM: hide@address.com");
		trigger_error("Uh oh, looks like we got an error in MySQL!<br />Logging this and emailing the admin<br />".mysql_error(), E_USER_ERROR);
		return false;
	}
}
?>
Return current item: Modular Site Manager