All pastes #674175 Raw Edit

Shaun

public php v1 · immutable
#674175 ·published 2007-08-28 23:48 UTC
rendered paste body
<?php/** *  * File: mysql.php * * Fire's up the mysql DB and includes several function as well as error reporting * */class Sql {	function connect ( $cfg ) {		$conn = mysql_connect($cfg['dbhost'], $cfg['dbuser'], $cfg['dbpass']);		if(!$conn) {			Sql::error("Can't find database", "The script is having trouble finding the database, make sure that MySQL is running");		}		if(!mysql_select_db($cfg['dbname'], $conn)) {			Sql::error("Can't connect to database: " .$cfg['dbname'], "The script can not connect to the database: " .$cfg['dbname']. ", make sure the name is correct and that the username and password is right");	 	} 	}	 	/**	 * Array's data from the database	 *	 * @param String $query	 * @return String $result	 */ 		function fetch_array ( $query ){	   	$result = array();	   	while(@$r = mysql_fetch_array($query)) array_push($result, $r);		return $result;	}		/**	 * Query's the database	 *	 * @param String $query	 * @return String $value	 */ 	function query ( $query ){		$value = mysql_query( $query ) or die(Sql::error("Mysql Query Failed", mysql_error()));		return $value;	}		/**	 * Fetch's data from the database	 *	 * @param String $query	 * @param String $no_error	 * @return String $value	 */ 		function fetch ( $query, $no_error=1 ){		if($no_error == 0){ //Stop showing an error if we are expecting it may cause one			$value = mysql_fetch_array( $query );		} else {			$value = mysql_fetch_array( $query ) or die(Sql::error("Mysql Fetch Array Failed", mysql_error()));		}		return $value;	}		/**	 * Display number of rows in database	 *	 * @param String $query	 * @return String $value	 */ 		function num ( $query ){		$value = mysql_num_rows( $query );		return $value;	}		/**	 * Insert data into the database	 *	 * @param String $table	 * @param String $data	 * @return String	 */ 		function insert ( $table, $data ){		$fields = implode(array_keys($data), ',');		$values = "'".implode(array_values($data), "','")."'";		$query = "INSERT INTO " .$table. " (" .$fields. ") VALUES (" .$values. ")";		Sql::query( $query );		return true;	}	/**	 * Updates database with new data	 *	 * @param String $table	 * @param String $data	 * @param String $where	 * @return String	 */ 	function update( $table, $data, $where){		$query = "UPDATE ".$table." SET ";		foreach($data as $var => $value){			if($i>0){				$query .= ", ";			}			$query .= $var. " = '" .$value. "'";			$i++;		}		$query .=" WHERE ".$where." LIMIT 1;";		Sql::query($query);				return true;	}		/**	 * Displays an error when a mysql expression fails	 *	 * @param String $title	 * @param String $msg	 * @return String echo	 */ 		function error ( $title, $msg )	{		echo "<div>";		echo "	<h2 style=\"color: red;\">" .$title. "</h2>";		echo "	<p>" .$msg. "</p>";		echo "</div>";		die();	}	}	// Activate DBSql::connect($cfg);?>