Home >Backend Development >PHP Tutorial >php mysql database class_PHP tutorial
This is a commonly used mysql database class. Download it and take a look.
class mySqlClass {//Define the entire database access class mySqlClass
var $querynum = 0;//Query times variable
//Open the server connection--------- -------------------------------------------------- ------------------------------------------//
function connect( $dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0) {
//mysql_connect -- Open a connection to the MySQL server
//mysql_pconnect -- Open a connection to the MySQL server Persistent connection
//mysql_pconnect() and mysql_connect() are very similar, but there are two main differences.
//First, when connecting, this function will first try to find a (persistent) connection that has been opened on the same host with the same user name and password. If found, it will return this connection ID without opening a new connection. .
//Secondly, the connection to the SQL server will not be closed when the script is executed. This connection will remain open for future use (mysql_close() will not close the connection established by mysql_pconnect()).
//Determine whether the persistent connection setting is enabled. If it is enabled, the persistent connection will be used. If it is not enabled, the normal connection will be used.
if($pconnect) {
if(!@mysql_pconnect($dbhost, $dbuser, $dbpw )) {
$this->halt('1');
}
} else {
if(!@mysql_connect($dbhost, $dbuser, $dbpw)) {
$this->halt('1');
}
}
//Set the character encoding if the database version is greater than 4.1 and the character set parameter in the global variable is not empty------ -------------------------------------------------- -//
if($this->version() > '4.1') {
//$charset = 'gbk';
$charset = 'utf-8';
$dbcharset = '';
if(!$dbcharset && in_array(strtolower($charset), array('gbk', 'big5', 'utf-8'))) {
$dbcharset = str_replace( '-', '', $charset);
}
//mysql_query -- Send a MySQL query
//Set the NAMES parameter to the corresponding character set parameter value to avoid occurrences when PHP reads the mysql database Chinese garbled characters
if($dbcharset) {
mysql_query("SET character_set_connection=$dbcharset, character_set_results=$dbcharset, character_set_client=binary");
}
//If the database version is greater than 5.0, set sql_mode The parameter is empty
if($this->version() > '5.0.1') {
mysql_query("SET sql_mode=''");
}
}
if($dbname) {//If the database name exists
//mysql_select_db -- Select MySQL database
//If the database selection is unsuccessful
mysql_select_db($dbname);
}
}
//mysql_select_db -- Select MySQL database------------------------------------------------ ----------------------------------//
function select_db($dbname) {
return mysql_select_db ($dbname);
}
//Read a data record function-------------------------- ------------------------------------------//
//mysql_fetch_array -- Get a row from the result set as an associative array, or a numeric array, or both
//The optional second parameter result_type in mysql_fetch_array() is a constant that can accept the following values: MYSQL_ASSOC, MYSQL_NUM and MYSQL_BOTH.
//MYSQL_ASSOC returns the data column using the field name as the index name of the array.
//MYSQL_BOTH The data column returned uses the field name and numeric index as the index name of the array.
//MYSQL_NUM The returned data column uses a numeric index as the index name of the array. The index starts at 0, indicating the first field of the returned result.
function fetch_array($query, $result_type = MYSQL_ASSOC) {
return mysql_fetch_array($query, $result_type);
}
//Query database function ------- -------------------------------------------------- ------------//
function query($sql, $type = '') {
$func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query' ) ?
'mysql_unbuffered_query' : 'mysql_query';
if(!($query = $func($sql)) && $type != 'SILENT') {
$this->halt( '0');
}
//Query times +1
$this->querynum++;
//Return query result resources
return $query;
}
//Return the number of affected records------------------------------------------------- ----------------------------//
//If the function is executed successfully, the number of recorded rows will be returned; if it fails, will return "-1".
function affected_rows() {
return mysql_affected_rows();
}
//Return the text error message generated by the previous MySQL operation------------ ----------------------------------//
function error() {
return mysql_error();
}
//Return the text error message integer type generated by the previous MySQL operation--------------------- ---------------//
function errno() {
return intval(mysql_errno());
}
/ /mysql_result -- Returns the field value in the result set---------------------------------------- -----//
function result($query, $row) {
$query = @mysql_result($query, $row);
return $query;
}
//mysql_num_rows -- Get the number of rows in the result set--------------------------------- ------------//
function num_rows($query) {
$query = mysql_num_rows($query);
return $query;
}
//mysql_num_fields -- Get the number of fields in the result set--------------------------------- ------------//
function num_fields($query) {
return mysql_num_fields($query);
}
//mysql_free_result -- Release the used result memory--------------------------------------------- ---//
function free_result($query) {
return mysql_free_result($query);
}
//mysql_insert_id -- Get the value generated by the previous INSERT operation ID------------------------------------------------//
function insert_id() {
$id = mysql_insert_id();
return $id;
}
//mysql_fetch_row -- Get a row from the result set as a numeric array ---------------------------------------------//
function fetch_row($query) {
$query = mysql_fetch_row($query);
return $query;
}
//mysql_field_name -- Get the field specified in the result Field name------------------------------------------------//
function field_name($query,$num=0){
return mysql_field_name($query,$num);
}
//mysql_fetch_field -- Get columns from the result set Information and returned as an object $num=0 indicates the first field---------------------------------//
function fetch_fields($query,$num=0) {
return mysql_fetch_field($query,$num);
}
//mysql_get_server_info -- Get MySQL server information--- ------------------------------------------//
function version() {
$MySqlVer = mysql_get_server_info();
if (empty($MySqlVer) || $MySqlVer==""){
$MySqlVer = $this->result(($ this->query("SELECT VERSION()")), 0);
}
if (empty($MySqlVer) || $MySqlVer==""){
$MySqlVer = '0 ';
}
return $MySqlVer;
}
//mysql_close -- Close the MySQL connection---------------- -------------------------------------------------- ---//
function close() {
return @mysql_close();
}
//Exception handling function halt ------ -------------------------------------------------- -------------//
function halt($mess) {
global $DataBaseBadFlag;
//$dberror = $this->error( );
//$dberrno = $this->errno(); //1114 personnel connection limit
if ($mess=="1"){
if ($DataBaseBadFlag!=1) {
echo("");
}
else{
if (!@function_exists ('phpEscape')){
require("escape.php");
}
echo phpEscape("Lock = false;location.href='pagedataerr.php';");
}
exit();
}
else{
return false;
}
}
}