MySQL database operation class, share to Everyone
[PHP]
/**
*db.class.php Create Databse Object
*
* @author Dick 417@668x.net
* @copyright Http://blog.csdn.net/haibrother
*
**/
Class dick_db{
Public $db _host = "; Host Address
Public $db _username = "; Database account
Public $db _password = "; Database Password
Public $db _name = "; Database name
Public $link = '; Database Connection Object
public $debug = 1; Whether to turn on debug debugging, which is turned on by default
Public $pconnect = 0; Whether to turn on long connections, which is off by default
Public $log _file = ' log/';//log file directory
/**
* Initialization Information
* @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 ();
}
/**
* Get Links
* */
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;
}
/**
* Get the last ID of insert
* @param string $sql
**/
Public Function insert_id () {
Return mysql_insert_id ($this->link);
}
/**
* Release Database object resources
**/
Public Function Free_result ($query) {
Return Mysql_free_result ($query);
}
/**
* Close Database connection
**/
Public function Close () {
if ($this->pconnect = = = 0) return mysql_close ($this->link);
}
/**
* Integer safe handling, returning only integers greater than or equal to 0
* @param int
* @return int
* */
Public Function numeric (& $variint) {
if (!isset ($variint))
return 0;
if (!is_numeric ($variint))
return 0;
First character 0 processing
$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);
}
Digital Security Processing
if ($str _len > 0) {
if (!preg_match ("/^[0-9]+$/", $variint)) {
return 0;
} else {
$variint = substr ($variint, 0, 10);
Compatible with the int unsigned maximum value in MySQL 4294967295
$variint = ($variint > 4294967295)? 4294967295: $variint;
return $variint;
}
} else {
return 0;
}
}
/**
* Return MySQL Error
**/
Public Function error () {
Return ($this->link) mysql_error ($this->link): Mysql_error ());
}
/**
* Return to MySQL errno
**/
Public Function errno () {
Return Intval ($this->link) Mysql_errno ($this->link): Mysql_errno ());
}
/**
* Write SQL error log
* @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. '-system without permission to read operation! ');
}
if (!is_writable ($file)) {
$this->halt ($file. '-system without permission to write! ');
}
$contents = file_get_contents ($file);
$add _message = '-error: '. $this->error (). '-errno: '. $this->errno ();
File_put_contents ($file, $contents. $message. $add _message. " \ n ");
}
/**
* Termination procedure
* @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 ";
}
$errmsg. = " Time: ". Date (' y-m-d h:i:s ')." \ n ";
$errmsg. = "Script: ". $error _get_last[' file ']." \ n ";
if ($sql) {
$errmsg. = "SQL: ". Htmlspecialchars ($sql)." \ n ";
}
$errmsg. = "Error: ". $this->error ()." \ n ";
$errmsg. = "Errno.: ". $this->errno ();
echo "\ n ";
echo "
";
echo nl2br ($errmsg);
Exit ();
}
}
/**
* Program Test Print
* @param string
* @return Print
**/
Public Function PRF ($param) {
Echo '
Echo '
';
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));
?>
http://www.bkjia.com/PHPjc/477537.html www.bkjia.com true http://www.bkjia.com/PHPjc/477537.html techarticle MySQL database operation class, share to everyone [PHP]? PHP/** *db.class.php Create databse Object * * @author Dick 417@668x.net * @copyright/http Blog.csdn.net/haibrother * **/class D ...