自己写的一个php 用mysqli操作数据库辅助类
<?php if (stristr ( $_SERVER ["PHP_SELF"], basename ( __FILE__ ) )) { exit ( 'forbidden' ); } require_once 'IDbHelper.php'; class MysqliHelper implements IDbHelper { private $host; private $user; private $password; private $dbname; private $port; private $charset; private $mysqli; function __construct($host, $user, $password, $dbname, $port = 3306, $charset = "utf-8") { $this->host = $host; $this->user = $user; $this->password = $password; $this->dbname = $dbname; $this->port = $port; $this->charset = $charset; $this->mysqli = new mysqli ( $host, $user, $password, $dbname,$port ); if ($this->mysqli->errno != 0) { echo "mysqli 连接错误:" . $this->mysqli->error; exit (); } } function init() { if ($this->mysqli == null) { $this->mysqli = new mysqli ( $this->$host, $this->$user, $this->$password, $this->$dbname,$this->port ); if ($this->mysqli->errno != 0) { echo "mysqli 连接错误:" . $this->mysqli->error; exit (); } } } /** * 返回查询结果集, 以 key 为键组织成关联数组, 每一个元素是一个对象. * 如果 key 为空, 则将结果组织成普通的数组. * $sql 必填 sql语句 * $params 可选参数,以sql参数名作为key的数组 */ public function query($sql,$arr=null) { $this->init(); $result = $this->mysqli->query ( $sql ); /* * $data = array(); while($row = $result->fetch_array(MYSQLI_ASSOC)){ $data[] = $row; } return $data; */ echo $this->mysqli->error; return $result; } // 以数组的方式返回结果的第一条记录 function getRow($sql,$arr=null) { $this->init(); $result = $this->mysqli->query ( $sql ); echo $this->mysqli->error; while ( $row = $result->fetch_array ( MYSQLI_ASSOC ) ) { return $row; } return null; } // 返回结果的第一条第一列记录 function getObject($sql,$arr=null) { $this->init(); $result = $this->mysqli->query ( $sql ); echo $this->mysqli->error; while ( $row = $result->fetch_array ( MYSQLI_NUM ) ) { return $row [0]; } return null; } // 执行sql语句 function exec($sql,$arr=null) { $this->init(); $count = $this->mysqli->query ( $sql ); echo $this->mysqli->error; return $this->mysqli->affected_rows; } public function data() { $this->init(); return $this->mysqli; } function Insert($tableName, $arr) { return 0; } function Update($tableName,$primary,$arr) { return 0; } function __destruct() { if ($this->mysqli) { //$this->mysqli->close (); $this->mysqli = null; } } } ?>
5/8/2014 4:58:04 PM 58.248.2....
测试......