PHP 简单的MySQLi数据库类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 简单的MySQLi数据库类相关的知识,希望对你有一定的参考价值。

/**
 * Database connection class.
 * $db = new db(array(host, user, password, database));
 * $db->connect();
 * $clean = $db->clean($dirty);
 * $db->query("");
 * $db->disconnect();
 */
class db {

	private $db = array();
	private $connection;

	public function db($args = array()) {
		$this->db['server']		= $args['server'];
		$this->db['username'] 	= $args['username'];
		$this->db['password'] 	= $args['password'];
		$this->db['database'] 	= $args['database']; }

	public function connect() {
		$this->connection = mysqli_connect($this->db["server"], $this->db["username"], $this->db["password"]);
		$this->select_db(); }

	public function disconnect() {
		mysqli_close($this->connection);
		$this->connection = null; }

	public function select_db() {
		mysqli_select_db($this->connection, $this->db["database"]); }

	public function query($sql) {
		$this->result = mysqli_query($this->connection, $sql); }

	public function is_connected() {
		return ($this->connection) ? true : false; }

	public function clean($dirty) {
		if (!is_array($dirty)) {
			$dirty = ereg_replace("[\'\")(;|`,<>]", "", $dirty);
			$dirty = mysqli_real_escape_string($this->connection, trim($dirty));
			$clean = stripslashes($dirty);
			return $clean; };
		$clean = array();
		foreach ($dirty as $p=>$data) {
			$data = ereg_replace("[\'\")(;|`,<>]", "", $data);
			$data = mysqli_real_escape_string($this->connection, trim($data));
			$data = stripslashes($data);
			$clean[$p] = $data; };
	return $clean; }

}

以上是关于PHP 简单的MySQLi数据库类的主要内容,如果未能解决你的问题,请参考以下文章

PHP数据访问(面向对象方式:mysqli类)

无法通过php代码连接到mysql数据库,PHP致命错误:找不到类'MySQLi'

PHP 数据库连接工具类(MySQLI函数包装)

php基础编程-php连接mysql数据库-mysqli的简单使用

php MySQLi数据库操作 封装类

PHP使用mysqli扩展库实现增删改查(面向对象版)