时间:2021-07-01 10:21:17 帮助过:1人阅读
[php]
/**
*db.class.php create databse object
*
*@author Dick 417@668x.net
*@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 = "System info: $message\n\n";
}
$errmsg .= "Time: ".date('Y-m-d H:i:s')."\n";
$errmsg .= "Script: ".$error_get_last['file']."\n\n";
if($sql) {
$errmsg .= "SQL: ".htmlspecialchars($sql)."\n";
}
$errmsg .= "Error: ".$this->error()."\n";
$errmsg .= "Errno.: ".$this->errno();
echo "\n";
echo "
";
echo nl2br($errmsg);
exit();
}
}
/**
*程序测试打印
*@param string
*@return print
**/
public function prf($param){
echo '
';';
print_r($param);
echo '
http://www.bkjia.com/PHPjc/477537.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477537.htmlTechArticlemysql 数据库操作类,分享给大家 [php] ?php /** *db.class.php create databse object * *@author Dick 417@668x.net *@copyright http://blog.csdn.net/haibrother * **/ class D...