Home  >  Article  >  Backend Development  >  A MYSQL operation class written by PHP

A MYSQL operation class written by PHP

不言
不言Original
2018-04-03 15:35:082603browse

This article shares with you a MYSQL operation class written in PHP. Friends who are interested can take a look at

Recommended mysql video tutorials: "mysql tutorial

Make a record here

Several knowledge points of the PHP class

self::$static property name

$this->General attribute name or method name

Four key points of singleton class

1 Prohibit the construction of objects---Prohibit new() private __construct(){} private empty definition

2 Prohibit cloning objects -- clone private __clone(){} Private empty definition

3 Define singleton object attributes private static $object name Private static empty definition

4Definition Entry method public static function method name () public static

<?php

class mysqldb
{
	public $cs=array(//连接数组
		"h"=>"127.0.0.1",
		"u"=>"root",
		"p"=>"",
		"db"=>"mysql",
		"pt"=>3306
		);

	public $arr;//取回的结果集存放在此数组中

	private static $con;//连接资源
	private static $result;//结果资源

	//连接方法
	public function connect()
	{
		self::$con=@mysqli_connect($this->cs[h],$this->cs[u],$this->cs[p],$this->cs[db],$this->cs[pt]);

		if ( !self::$con )	
		{
			die( &#39;<br>连接错误 (&#39;.mysqli_connect_errno().&#39;) &#39;.mysqli_connect_error());
		}
	}
	//SQL命令执行方法
	public function query($sql)
	{
		
		if(!self::$result=@mysqli_query(self::$con,$sql))//执行SQL命令
		{
			if(@mysqli_errno())//执行错误编号判断
			{//有错误编号
				echo "<br>*****************************";
				echo "<br />执行失败!!!";
				echo "<br>所执行的SQL命令:$sql";
				echo "<br />错误号:(".mysqli_errno().")";
				echo "<br />错误信息:".mysqli_error();
				echo "<br>*****************************";
				mysqli_free_result ( $this->result );//释放结果资源
			}
			else
			{//没有错误编号
				echo "<br>所执行的SQL命令:$sql";
				echo "<br>".$this->cs[&#39;db&#39;]."数据库中不存在此表";
			}
			mysqli_close ( $this->con );//关闭连接资源
			die();
		}
		
	}
	//设置字符集 set names utf8
	public function charset($charset="utf8")
	{
		$a="set names ".$charset;
		$this->query($a);
	}
	//打开/选择数据库:USE
	public function selectdb($dbname="")
	{
		if($dbname=="")
		{
			$dbname=$this->cs[&#39;db&#39;];
		}
		$a="use ".$dbname;
		$this->query($a);
	}
	//执行前的准备 连接 设置连接字符 打开数据库
	public function queryfun()
	{
		if(!self::$con)//如果连接资源为NULL
		{
			$this->connect();
		}
		else
		{
			echo "<br>已经建立连接,不必再建立";
		}
		$this->charset();//设置字符
		$this->selectdb();//打开数据库

	}

	//执行SQL命令,并将远程结果集存放在本地数组$this->arr.
	public function queryarr($sql)
	{
		
		$this->queryfun();//执行前准备
		$this->query($sql);//执行命令
		if(self::$result)//如果有执行结果集就取出存为本地数组
		{
			$this->arr=mysqli_fetch_all(self::$result,MYSQLI_ASSOC);//取得所有,字段名下标的数组
			mysqli_free_result (self::$result);//释放数据库服务器的结果资源
		}
		mysqli_close (self::$con);//断开数据库服务器的连接
		self::$result=null;//复位
		self::$con=null;//复位
	}

	public function tab($arr)
	{
		echo "<table border=&#39;1&#39;>";
		foreach($arr as $k1=>$v1)
		{				
			//**********************
			//表头
			if($k1==0)
			{
				echo "<tr>";
				foreach($v1 as $k2=>$v2)
				{
					
					echo "<th>";
					echo $k2;
					echo "</th>";
				}
				echo "</tr>";
			}
			//**********************
			//内容
			echo "<tr>";
			foreach($v1 as $k2=>$v2)
			{
				
				echo "<td>";
				echo $v2;
				echo "</td>";
			}
			echo "</tr>";
		}
		echo "</table>";

	}

	public function querytab($sql)
	{
		$this->queryfun();//执行前准备
		$this->query($sql);//执行命令
		if(self::$result)//如果有执行结果集就取出存为本地数组
		{
			$this->arr=mysqli_fetch_all(self::$result,MYSQLI_ASSOC);//取得所有带下标的数组

			mysqli_free_result (self::$result);//释放数据库服务器的结果资源
			mysqli_close (self::$con);//断开数据库服务器的连接
			self::$result=null;//复位
			self::$con=null;//复位
		
			//return $arr;
			echo "<table border=&#39;1&#39;>";
			foreach($this->arr as $k1=>$v1)
			{				
				//**********************
				//表头
				if($k1==0)
				{
					echo "<tr>";
					foreach($v1 as $k2=>$v2)
					{
						
						echo "<th>";
						echo $k2;
						echo "</th>";
					}
					echo "</tr>";
				}
				//**********************
				//内容
				echo "<tr>";
				foreach($v1 as $k2=>$v2)
				{
					
					echo "<td>";
					echo $v2;
					echo "</td>";
				}
				echo "</tr>";
			}
			echo "</table>";

			return true;

		}
		mysqli_close(self::$con);//断开数据库服务器的连接
		self::$result=null;//复位
		self::$con=null;//复位

		return false;
	}


	//*********************************
	//***单例化
	//private
	private function __construct(){}//令new()新对象失效
	private function __clone(){}//令克隆对象失效.

	private static $db;//单例对像属性
	
	public static function getdb()//单例对象入口
	{
		if(!isset(self::$db))
		{
			self::$db=new self();
		}
		return self::$db;
	}
}
//******************************
//**********调用例子************
//******************************
/*
$k=mysqldb::getdb();
$k->cs=array(
		"h"=>"127.0.0.1",
		"u"=>"root",
		"p"=>"",
		"db"=>"mysql",
		"pt"=>3306
		);
$k->queryarr("select * from user;");
$k->tab($k->arr);
echo "<hr />";
$k->querytab("select * from user;");

*/

?>

The above is the detailed content of A MYSQL operation class written by PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn