<?php
/**
*
* 公用函数库
*
* @功能:
*
* 常用的公用函数
*
* @version 0.0.1
* @author HH
* @link #
* @QICQ #
*
*/
/**
* 测试输出语句
*
* @access public
* @param mixed $val
* @param bool $mark
* @return void
*/
function pf($val,$mark = false){
print("<pre>");
$mark === false && print_r($val) || var_dump($val);
}
/**
* 验证输入的邮件地址是否合法
*
* @access public
* @param string $email 需要验证的邮件地址
*
* @return bool
*/
function is_email($user_email)
{
$chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i";
if (strpos($user_email, '@') !== false && strpos($user_email, '.') !== false)
{
if (preg_match($chars, $user_email))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
/**
* 返回IP地址
*
* @access public
* @param bool $int
* @return void
*/
function getIp($int = false){
return $int === false ? $_SERVER['SERVER_ADDR'] : ip2long($_SERVER['SERVER_ADDR']);
}
/**
* 返回时间
*
* @access public
* @param bool $str
* @return void
*/
function getTime($str = false){
return $str === false ? $_SERVER['REQUEST_TIME'] : date('Y-m-d H:i:s',$_SERVER['REQUEST_TIME']);
}
?>
<?php
/**
*
* 数据库配置文件
*
* @功能:
*
* 数据库详细参数设置
*
* @version 0.0.1
* @author HH
* @link #
* @QICQ #
*
*/
$config = array(
'dbname' => 'test',//待连接的数据库名字
'server' => 'localhost',//待连接的数据库地址
'user' => 'root',//待连接的数据库用户名
'password' => '123456',//待连接的数据库密码
'reporterror' => '0',//是否输出错误
'dbport' => '3306' //待连接的数据库端口
);
?>
<?php
/**
*
* 数据库操作类
*
* @功能:
*
* 操作数据库方法
*
* @version 0.0.1
* @author HH
* @link #
* @QICQ #
*
*/
class DB{
private $database = '';
private $link_id = 0;
private $query_id = null;
private $record = array();
private $errdesc = '';
public $errno = 0;
private $reporterror = 1;
private $server = 'localhost';
private $user = 'root';
private $password = '';
private $counts = 0;
private $dbport=3306;
private $dbname="BY";
/**
* 测试输出语句
*
* @access public
* @param mixed $val
* @param bool $mark
* @return void
*/
public function __construct($config = array())
{
if (!is_array($config))
{
throw new Exception('配置参数必须为数组');
}
if (isset($config['dbname']) && $config['dbname'] != '')
{
$this->database = $config['dbname'];
}
if (isset($config['server']) && $config['server'] != '')
{
$this->server = $config['server'];
}
if (isset($config['user']) && $config['user'] != '')
{
$this->user = $config['user'];
}
if (isset($config['password']) && $config['password'] != '')
{
$this->password = $config['password'];
}
if (isset($config['reporterror']) && $config['reporterror'] != '')
{
$this->reporterror = $config['reporterror'];
}
if (isset($config['dbport']) && $config['dbport'] != '')
{
$this->dbport = $config['dbport'];
}
}
/**
* 数据库连接
*
* @access public
* @return void
*/
public function connect()
{
// connect to db server
if ( 0 == $this->link_id )
{
$this->link_id=@mysqli_connect($this->server,$this->user,$this->password,$this->database,$this->dbport);
if (!$this->link_id) { $this->halt("MysqlI数据库连接失败."); }
$this->query("SET NAMES 'utf8'");
if ($this->database!="")
{
if(!mysqli_select_db($this->link_id,$this->database))
{
$this->halt("cannot use database ".$this->database);
}
}
unset($this->password);
}
}
/**
* 获取数据库错误信息
*
* @access public
* @return string 错误信息
*/
public function geterrdesc()
{
$this->error=@mysqli_error($this->link_id);
return $this->error;
}
/**
* 获取数据库错误编号
*
* @access public
* @return int 错误编号
*/
public function geterrno()
{
$this->errno=@mysqli_errno($this->link_id);
if(in_array($this->errno,array(2006, 2013))){
connect();
}else{
return $this->errno;
}
}
/**
* 选择待操作的数据库
*
* @access public
* @return void
*/
public function select_db($database="")
{
if ($database!="") { $this->database=$database; }
if(!mysqli_select_db($this->link_id,$this->database)) {
$this->halt("无法使用数据库 ".$this->database);
}
}
/**
* 执行数据库操作
*
* @access public
* @param string SQL语句
* @return mixed 执行结果集
*/
public function query($query_string)
{
$array=$this->query_id = mysqli_query($this->link_id,$query_string);
if (!$this->query_id) { $this->halt("无效 SQL: ".$query_string); }
$this->record[] = $query_string;
$this->counts++;
return $this->query_id;
}
/**
* 获取查询结果集
*
* @access public
* @param mixed 操作结果集
* @return array 查询结果集二维数组
*/
public function fetch_array($query_id=-1)
{
if (!empty($query_id)) { $this->query_id=$query_id; }
$this->record = mysqli_fetch_array($this->query_id,MYSQLI_ASSOC);
return $this->record;
}
/**
* 生成二维数组
*
* @access public
* @return array 查询结果集二维数组
*/
public function to_array()
{
$rs = array();
while (($rtn = mysqli_fetch_array($this->query_id, MYSQLI_ASSOC)) !== false)
{
$rs[] = $rtn;
}
return $rs;
}
/**
* 释放操作资源
*
* @access public
* @param mixed 操作结果集
* @return bool 释放是否成功
*/
public function free_result($query_id=-1)
{
if (!empty($query_id)) { $this->query_id=$query_id; }
return @mysqli_free_result($this->query_id);
}
/**
* 查询一行记录
*
* @access public
* @param string 操作语句
* @return array 操作结果集
*/
public function query_first($query_string)
{
$this->query($query_string);
$returnarray=$this->fetch_array();
$this->free_result($this->query_id);
return $returnarray;
}
/**
* 调整结果集中的下表位置
*
* @access public
* @param int 下标位置
* @param mixed 操作结果集
* @return bool 返回操作是否成功
*/
public function data_seek($pos,$query_id=-1) {
// goes to row $pos
if ($query_id!=-1) { $this->query_id=$query_id; }
$status = mysqli_data_seek($this->query_id, $pos);
return $status;
}
/**
* 操作结果集影响行数
*
* @access public
* @param mixed 操作结果集
* @return int 操作影响行数
*/
public function num_rows($query_id=-1) {
// returns number of rows in query
if ($query_id!=-1) { $this->query_id=$query_id; }
return mysqli_num_rows($this->query_id);
}
/**
* 获取影响行数
*
* @access public
* @return int 影响行数
*/
public function affect_rows()
{
return mysqli_affected_rows($this->link_id);
}
/**
* 执行插入操作后,获取最新插入ID
*
* @access public
* @return int 最新插入ID
*/
public function insert_id() {
// returns last auto_increment field number assigned
return mysqli_insert_id($this->link_id);
}
/**
* 终止数据库操作
*
* @access public
* @param string 错误语句
* @return void
*/
public function halt($msg) {
if(in_array(mysqli_errno($this->link_id),array(2006, 2013))){
$this->connect();
}else{
$this->errno=@mysqli_errno($this->link_id);
$this->errdesc=@mysqli_error($this->link_id);
//require_once('salebombmailer/mail.inc.php');
//global $technicalemail;
$message="数据库错误 : $msg\r\n<br />";
$message.="MySQLI错误: $this->errdesc\r\n<br />";
$message.="MySQLI错误号: $this->errno\r\n<br />";
$message.="日期: ".date("l dS of F Y h:i:s A")."\r\n<br />";
$message.="脚本: ".getenv("REQUEST_URI")."\r\n<br />";
$message.="附注: ".getenv("HTTP_REFERER")."\r\n<br />";
//$mail->Subject = "数据库错误"; //必填,邮件标题(主题)
//$mail->MsgHTML($message); //邮件正文内容,提取html文件为其内容
//$mail->AddAddress($technicalemail, "系统数据库错误信息"); //收件人地址。参数一:收信人的邮箱地址,可添加多个。参数二:收件人称呼
//$mail->Send();
@mail ("812045247@qq.com","数据库错误!",$message,"From: \"\" <xiao_mao2008@yahoo.com.cn>");
if ($this->reporterror)
{
// // throw new DBException($message);
echo "系统繁忙";
exit;
}
}
}
/**
* 开始事物
*
* @access public
* @return void
*/
public function beginTransaction()
{
$this->query('begin');
}
/**
* 提交事物
*
* @access public
* @return void
*/
public function commit()
{
$this->query('commit');
}
/**
* 撤销事物/事物回滚
*
* @access public
* @return void
*/
public function rollback()
{
$this->query('rollback');
}
}
<?php
/**
*
* 公用头文件
*
* @功能:
*
* 公共引入的头文件,集合常用的变量函数
*
* @version 0.0.1
* @author HH
* @link #
* @QICQ #
*
*/
require './common.php';//引入公共方法操作文件
require './db.ini.php';//引入数据库配置文件
require './db_mysql.php';//引入数据库操作文件
$ip = getIp(true);
$time = getTime(true);
$dbObj = new DB($config);
$dbObj->connect();
?>
<?php
require './initial.php';
$resource = $dbObj->query("SELECT * FROM `user`");
$rs = array();
while($ret = $dbObj->fetch_array($resource)){
$rs[] = $ret;
}
pf($time);
?>