迷你mysql操作類

使用方法:

<?php

 require "gridtable.css"; 
 require "mydb_class.php";
 
 $DB = new mydb();
 $RS = new myrs();
 $DB->conn('localhost','root','root');
 $DB->open('caigou');
 $DB->query('SET NAMES UTF8');
 $DB->query('SELECT * from caigou');
 $RS->load($DB->rsl);
 $RS->maketable('class="gridtable"','title','lis');
 $DB->close();
?>

<?php

	Class myrs{
		var $rsl;
		var $row;
		var $fld;
		var $finfo;
		
		function load($rsl) {
			$this->rsl = $rsl;
			$this->seek(0);
			$this->fld = @mysqli_num_fields($this->rsl)
				or die('無法解析記錄集結構。');
			$this->finfo = @mysqli_fetch_fields($this->rsl)
				or die('無法解析記錄集結構。');
				
		}
		
		function seek($recno) {
			@mysqli_data_seek($this->rsl,$recno)
				or die('無法移動記錄集指針。');
			$this->fetch_row()
				or die('無法獲取記錄集表頭結構。');			
		}
		
		function fetch_row() {
			$this->row = @mysqli_fetch_row($this->rsl);
			return $this->row;
		}
		
		function lis() {
			while($this->row) {
				$this->disp();
			}
		}
		
		function maketable(...$cmds) {
			$showcss = false;
			$showtit = false;
			$showlis = false;
			$strcss = '';
			
			foreach($cmds as $val) {
				if($val == 'title')	$showtit = true;
				if($val == 'lis')	$showlis = true;
				if(strpos($val,'class') !== false){
					$showcss = true;
					$strcss = $val;
				}
			}
			echo "
\n"; } function title() { echo "\t\n"; foreach ($this->finfo as $val) { echo "\t\t".$val->name."\n"; //if ($val->type==10) echo 'date'; } echo "\t\n"; } function disp() { echo "\t\n"; for($i=0;$i<$this->fld;$i++) echo "\t\t".$this->row[$i]."\n"; echo "\t\n"; $this->fetch_row(); } } Class mydb { var $url; var $uid; var $pwd; var $tid; var $con; var $fun; var $rsl; function conn($url,$uid,$pwd) { $this->fun = 'conn'; $this->url = $url; $this->uid = $uid; $this->pwd = $pwd; $this->con = @mysqli_connect($this->url,$this->uid,$this->pwd) or $this->showerr('連接數據庫失敗。'); return $this->con; } function open($tid) { $this->fun = 'open'; $this->tid = $tid; @mysqli_select_db($this->con,$this->tid) or $this->showerr('無法打開庫。'); } function close() { $this->fun = 'close'; @mysqli_close($this->con) or $this->showerr('在關閉數據庫時發生錯誤。'); @mysqli_free_result($this->rsl); } function query($sql) { $this->fun = 'query'; $this->rsl = @mysqli_query($this->con,$sql) or $this->showerr('查詢數據庫失敗。在命令:'.$sql); return $this->rsl; } function showerr($msg) { $this->fun = 'showerr'; echo '錯誤:'.$msg.'
'; die(-1); } } ?>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章