首页 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

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++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 教程
1321
25
PHP教程
1269
29
C# 教程
1249
24