Location: PHPKode > scripts > MySQL > mysql-3045/mysql.cls.php
<?
/* vim: set fdm=marker: */
require_once(dirname(__FILE__)."/misc.php");
class mysql{
	var $dsn;
	var $link;
	function mysql($dsn=''){/*{{{*/
		if(strlen($dsn)>0){
			preg_match("/(\w*):\/\/(\w*):(\w*)@(.*)\/(\w*)/",$dsn,$matches);
			$user=$matches[2];
			$pass=$matches[3];
			$host=$matches[4];
			$db=$matches[5];
			$this->link=mysql_connect($host,$user,$pass);
			$this->dsn=$dsn;
			mysql_select_db($db);
		}
	}/*}}}*/
	function getone($query){/*{{{*/
//		$rs=$this->query($query." limit 1"); //error where query end with limit 10 etc
		$rs=$this->query($query);
		if($rs_array=mysql_fetch_array($rs)){
			mysql_free_result($rs);
			return $rs_array[0];
		}
	}/*}}}*/
	function getcol($query){/*{{{*/
		$array=array();
		$rs=$this->query($query);
		while($rs_array=mysql_fetch_row($rs)){
			$array[]=$rs_array[0];
		};
		mysql_free_result($rs);
		return $array;
	}/*}}}*/
	function getrow($query){/*{{{*/
//		if(!preg_match("/limit \d+(,\d+)?/i",$query)){
//			$query=$query." limit 1";
//		}
		$rs=$this->query($query);
		while($rs_array=mysql_fetch_array($rs,MYSQL_ASSOC)){
			mysql_free_result($rs);
			return $rs_array;
			break;
		};
		mysql_free_result($rs);
		return false;
	}/*}}}*/
	function getall($query){/*{{{*/
		$array=array();
		$rs=$this->query($query);
		while($rs_array=mysql_fetch_array($rs,MYSQL_ASSOC)){
			$array[]=$rs_array;
		};
		mysql_free_result($rs);
		return $array;
	}/*}}}*/
	function execute($query){/*{{{*/
		$rs=$this->query($query);
		return $rs;
	}/*}}}*/
	function query($query){/*{{{*/
		if(strlen($query)>0){
		$start_time=microtime_float();
		$rs=@mysql_query("$query",$this->link);
		$_SESSION["yao"]["num"]++;
		if (!$rs) {
			if(is_cli()){
				ob_start();
				debug_print_backtrace();
				$error= ob_get_contents();
				ob_end_clean();
				$error=preg_replace("/.*simpletest.*\n?/i","",$error);
				echo $error;
				require 'Console/Color.php';
//				print Console_Color::convert("%bHello World!%n\n"));
				die("Invalid query:".
				Console_Color::convert("%g".$query."%n")."\n".
				Console_Color::convert("%p"hide@address.com()."%n")
				);
			}else{
				echo "<pre>";
				if(function_exists("debug_print_backtrace")){
					@debug_print_backtrace();
				}else{
					var_dump(debug_backtrace());
				}
				echo "</pre>";
	//			print_a(debug_backtrace());
				die("Invalid query:<font color='red'>$query</font>;<br />"hide@address.com());
			}
		}
		$end_time=microtime_float();
		$run_time=$end_time-$start_time;
//		set_debug_query($query,$run_time);
		return $rs;
		
		}
	}/*}}}*/
	function sql_num($query){/*{{{*/
//		set_debug_query($query);
		$rs=$this->query($query);
		$num=mysql_num_rows($rs);
		return $num;
	}/*}}}*/
	function batch_execute($querys){/*{{{*/
//		set_debug_query($querys);
		foreach($querys as $query){
			if(strlen(trim($query))>0){
				$this->execute($query);
			}
		}
	}/*}}}*/
	function close(){/*{{{*/
		mysql_close($this->link);
	}/*}}}*/
}
?>
Return current item: MySQL