mysql 數據庫操作類,分享給大家
[php]
<?php
/**
*db.class.php create databse object
*
*@author Dick [email protected]
*@copyright http://blog.csdn.net/haibrother
*
**/
class Dick_Db{
public $db_host = ''; //主機地址
public $db_username = ''; //數據庫帳號
public $db_password = ''; //數據庫密碼
public $db_name = ''; //數據庫名
public $link = ''; //數據庫連接對象
public $debug = 1; //是否開啟debug調試,默認是開啟的
public $pconnect = 0; //是否開啟長連接,默認是關閉的
public $log_file = 'log/';//日志文件目錄
/**
*初始化信息
*@param object
**/
public function __construct($config=''){
if(!is_object($config)){
$this->halt('database config is not wrong,please check it!');
}
if(!empty($config)){
$this->db_host = $config->host;
$this->db_username = $config->username;
$this->db_password = $config->password;
$this->db_name = $config->dbname;
}
$this->connect();
}
/**
* 獲取鏈接
* */
public function connect(){
$connect = $this->pconnect === 1?'mysql_pconnect':'mysql_connect';
if(!$this->link = @$connect($this->db_host,$this->db_username,$this->db_password)){
$this->halt('Database cant not connect!');
}
mysql_set_charset('utf8',$this->link);
mysql_select_db($this->db_name,$this->link);
}
/**
*query
*@param string $sql
*return boolean
**/
public function query($sql){
if(!$query = mysql_query($sql,$this->link)){
$message = date('Y-m-d H:i:s').' '.$sql;
$this-> write_log($message);
$this->halt('SQL error,please check it!',$sql);
}
return $query;
}
/**
*
*@param string $sql
*@param string $type
* mysql_fetch_assoc mysql_fetch_array mysql_fetch_row mysql_affected_rows
*@return array
*/
public function fetch($sql,$type='assoc'){
$fetch_type = 'mysql_fetch_'.$type;
$query = $this->query($sql);
$result = $fetch_type($query);
$this->free_result($query);
$this->close();
return $result;
}
/**
*@param string $sql
*@return array
**/
public function dickFetch($sql,$type='assoc'){
$fetch_type = 'mysql_fetch_'.$type;
$query = $this->query($sql);
$rows=array();
while ($row=$fetch_type($query)) {
$rows[]=$row;
}
$this->free_result($query);
$this->close();
return $rows;
}
/**
*取得insert 最後一次的ID
*@param string $sql
**/
public function insert_id(){
return mysql_insert_id($this->link);
}
/**
*釋放數據庫對象資源
**/
public function free_result($query){
return mysql_free_result($query);
}
/**
*關閉數據庫連接
**/
public function close(){
if($this->pconnect === 0) return mysql_close($this->link);
}
/**
* 整數安全處理,僅返回大於等於0的整數
*@param int
*@return int
* */
public function numeric(& $variint){
if (!isset ($variint))
return 0;
if (!is_numeric($variint))
return 0;
//首字符0處理
$str_len = strlen($variint);
for ($i = 0; $i < $str_len; $i++) {
if ($variint[$i] != '0')
break;
}
if ($i > 0 && $variint > 0) {
$variint = substr($variint, $i, $str_len);
$str_len = strlen($variint);
}
//數字安全處理
if ($str_len > 0) {
if (!preg_match("/^[0-9]+$/", $variint)) {
return 0;
} else {
$variint = substr($variint, 0, 10);
//兼容MYSQL中INT無符號最大值4294967295
$variint = ($variint > 4294967295) ? 4294967295 : $variint;
return $variint;
}
} else {
return 0;
}
}
/**
*返回mysql error
**/
public function error() {
return (($this->link) ? mysql_error($this->link) : mysql_error());
}
/**
*返回mysql errno
**/
public function errno() {
return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
}
/**
*寫入SQL錯誤日志
*@param string
*@param string type
**/
public function write_log($message='',$type='date'){
if(empty($message))return false;
if(!in_array($type, array('date','month')))return false;
if(!is_dir($this->log_file)){
mkdir($this->log_file);
}
switch ($type) {
case 'month':
$file = $this->log_file.date('Y-m').'.log';
break;
default:
$file = $this->log_file.date('Y-m-d').'.log';
break;
}
if(!file_exists($file)){
file_put_contents($file,'');
}
if(!is_readable($file)){
$this->halt($file.'- 系統無權限讀操作!');
}
if(!is_writable($file)){
$this->halt($file.'- 系統無權限寫操作!');
}
$contents = file_get_contents($file);
$add_message = ' -Error:'.$this->error().' -Errno:'.$this->errno();
file_put_contents($file, $contents.$message.$add_message."\n");
}
/**
*終止程序
*@param str $message
*@return print
**/
public function halt($message='',$sql=''){
if($this->debug===1){
$error_get_last = error_get_last();
if($message) {
$errmsg = "<b>System info</b>: $message\n\n";
}
$errmsg .= "<b>Time</b>: ".date('Y-m-d H:i:s')."\n";
$errmsg .= "<b>Script</b>: ".$error_get_last['file']."\n\n";
if($sql) {
$errmsg .= "<b>SQL</b>: ".htmlspecialchars($sql)."\n";
}
$errmsg .= "<b>Error</b>: ".$this->error()."\n";
$errmsg .= "<b>Errno.</b>: ".$this->errno();
echo "</table></table></table></table></table>\n";
echo "<p style=\"font-family: Verdana, Tahoma; font-size: 11px; background: #FFFFFF;\">";
echo nl2br($errmsg);
exit();
}
}
/**
*程序測試打印
*@param string
*@return print
**/
public function prf($param){
echo '<pre>';
print_r($param);
echo '</pre>';
exit;
}
}
#################################################
####測試程序
####
##################################################
error_reporting(E_ALL);
header('Content-type:text/html;charset=utf-8');
date_default_timezone_set('Asia/Shanghai');
$config = array(
'host' => '192.168.2.1',
'username' => 'root',
'password' => '',
'dbname' => 'test',
);
$db = new Dick_Db((object)$config);
$db ->pconnect = 1;
$sql = 'SELECT * FROM t1';
$db->prf($db->dickFetch($sql));
?>