首頁 php教程 PHP源码 链式调用封装MySQL类

链式调用封装MySQL类

May 25, 2016 pm 05:08 PM

代码

<?php
/*Created By RexLee
**PHP file MySQL.php 2012-12-19 
*/

class MySQL {
	private $host;
	private $name;
	private $password;
	private $dbname;		//数据库名
	private $link;
	private $errlog=array();
	public $errreport=true;//开启错误报告
	private $runStat;//运行状态,记录当前运行的成员
	private $value;//结果
	
	public function __construct($server,$dbuser,$psw){//连接主机
		$this->host=$server;
		$this->name=$dbuser;
		$this->password=$psw;
		$this->link=@mysql_connect($this->host,$this->name,$this->password) or die(&#39;<font color="red">ERROR: <b><em>数据库主机连接失败!<em></b></font><br/>&#39;);
		$this->runStat="init";//定义为初始化
		$this->fn=0;
	}
	
	public function to2DArray(){//构造二维数组
		/////////////检测//////////////////
		if(!is_resource($this->value)){
			array_push($this->errlog, "ERROR: <em>运行错误,在<font color=\"red\"><b>to2DArray</b></font>层调用错误的方法<em><br/>");
			$this->runStat="error";//将运行状态设置为错误
			return $this;
		}
		$this->runStat="to2DArray";
		/////////////////////////////////////
		$_2DArray=Array();
		while($row=@mysql_fetch_array($this->value)){//直接取value值,此时的value应当为resource类型
			$keyarr=array_keys($row);
			foreach ($keyarr as $key) {
				if (is_int($key)) {
					unset($row[$key]);//去掉以数组为键名的数组元素
				};
			}
			array_push($_2DArray, $row);
		}
		$this->value=$_2DArray ;
		return $this;
	}
	
	public function db($database,$charset){//连接数据库
		/////////////检测//////////////////
		if(!is_resource($this->link)){
			array_push($this->errlog, "ERROR: <em>运行错误,在<font color=\"red\"><b>db</b></font>层调用错误的方法<em><br/>");
			$this->runStat="error";//将运行状态设置为错误
			return $this;
		}
		$this->runStat="db";
		/////////////////////////////////////
		$this->dbname=$database;
		mysql_query("set names ".$charset);//设置字符集
		$this->value=mysql_select_db($this->dbname,$this->link);
		if (!$this->value) {
			echo "<font color=\"blue\"><b>ERROR:打开数据库错误!</b></font><br/>";
			$this->runStat="error";
		}
		return $this;
	}
	public function query($sql) {
		/////////////检测//////////////////
		if(!is_string($sql)){
			array_push($this->errlog,"ERROR: <em>运行错误,在<font color=\"red\"><b>query</b></font>层调用错误的方法<em><br/>");
			$this->runStat="error";//将运行状态设置为错误
			return $this;
		}
		$this->runStat="query";
		/////////////////////////////////////
		$sql=addslashes($sql);//防注入
		$result=mysql_query($sql);
		if (!$result) {
			array_push($this->errlog,"ERROR: <em><b style=\"color:red;\">语句运行错误!</b></em></br>");
			$this->runStat="sqlerror";
		}
		$this->value=$result;
		return $this;
	}
	
	function value() {
		$this->runStat="init";
		if ($this->errreport) {
			foreach ($this->errlog as $value) {
				echo $value;
			}
		}
		$this->errlog=array();//clean
		$result=$this->value;
		$this->value=null;//clean
		return $result;
	}
	public function __call($f,$v){//错误方法吸收
		echo "<font color=\"red\"><b>ERROR</b></font>: 不存在".$f."()方法. <br/>";
		return $this;
	}
}
$db=new MySQL("localhost", "root", "lijun");
//$db->errreport=false;
$h=$db->db("students", &#39;utf8&#39;);
var_dump($db->db("ip", &#39;utf8&#39;)->to2DArray()->query("select * from ip limit 0,2")->to2DArray()->value());
var_dump($h->query("seslect * from nuser limit 10,2")->to2DArray()->value());
var_dump($db->db("ip", &#39;utf8&#39;)->query("SELECT * FROM ip LIMIT 0 , 2")->to2DArray()->value());

?>
登入後複製
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1423
52
Laravel 教程
1317
25
PHP教程
1268
29
C# 教程
1242
24