PHP MySQL Database class _php tutorial

Source: Internet
Author: User
Tags mysql query pconnect php mysql
This is a common use of MySQL database class Oh, download down to see it.

Class Mysqlclass {//define the entire database access class Mysqlclass
var $querynum = 0;//Query count variable

Open the connection to the server------------------------------------------------------------------------------------------------------ //
Function Connect ($dbhost, $dbuser, $DBPW, $dbname = ", $pconnect = 0) {
Mysql_connect--Open a connection to the MySQL server
Mysql_pconnect--Open a persistent connection to the MySQL server
Mysql_pconnect () and mysql_connect () are very similar, but there are two main differences.
First, when connecting, this function will first attempt to find a (persistent) connection with the same user name and password on the same host, and if found, return this connection identity without opening a new connection.
Second, when the script finishes executing the connection to the SQL server is not closed, the connection will remain open for later use (Mysql_close () will not close the connection established by Mysql_pconnect ().
Determine if persistent connection settings are turned on, use a persistent connection when on, or use a generic connection if not turned on
if ($pconnect) {
if (! @mysql_pconnect ($dbhost, $dbuser, $DBPW)) {
$this->halt (' 1 ');
}
} else {
if (! @mysql_connect ($dbhost, $dbuser, $DBPW)) {
$this->halt (' 1 ');
}
}
Set 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 garbled text when PHP reads MySQL database
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 the Sql_mode parameter to NULL
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 you select a database that is not successful
mysql_select_db ($dbname);
}
}

//mysql_select_db--Select MySQL database---------------------------------------------------------------------//
function select_db ($dbname) {
return mysql_select_db ($dbname);
}

//Read a data logging function---------------------------------------------------------------------//
//mysql_fetch_array- -Take a row from the result set as an associative array, or as a numeric array, or both with the optional second parameter in
//mysql_fetch_array () result_type is a constant that can accept the following values: Mysql_assoc,mysql_num and Mysql_both. The data column returned by the
//mysql_assoc uses a field name for the index of the array. The data column returned by the
//mysql_both uses the field name and the numeric index as the index name for the array. The data column returned by the
//mysql_num uses a numeric index as the index name for the array. The index starts at 0 and represents the first field that returns the 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 ');
}
//Queries +1
$this->querynum++;
//Return query result resource
return $query;
}

Returns the number of affected records---------------------------------------------------------------------//
If the function executes successfully, the number of record rows is returned, or "1" if it fails.
function Affected_rows () {
return Mysql_affected_rows ();
}

Returns the text error message generated by the previous MySQL operation---------------------------------------------//

Function error () {
return Mysql_error ();
}

Returns the text error message generated by the previous MySQL operation integer pattern------------------------------------//

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--Number of rows obtained in the result set---------------------------------------------//

function Num_rows ($query) {
$query = mysql_num_rows ($query);
return $query;
}

Mysql_num_fields--Gets the number of fields in the result set---------------------------------------------//

function Num_fields ($query) {
Return Mysql_num_fields ($query);
}

Mysql_free_result--Release the result used by memory---------------------------------------------//

function Free_result ($query) {
Return Mysql_free_result ($query);
}

MYSQL_INSERT_ID--Gets the ID generated by the insert operation in the previous step---------------------------------------------//

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--Gets the field name of the field specified in the result---------------------------------------------//

function Field_name ($query, $num =0) {
Return Mysql_field_name ($query, $num);
}

Mysql_fetch_field--Gets column information from the result set and returns as an object $num =0 represents 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 MySQL connection---------------------------------------------------------------------//

function Close () {
return @mysql_close ();
}


Exception handling function halt---------------------------------------------------------------------//

function Halt ($mess) {
Global $DataBaseBadFlag;
$dberror = $this->error ();
$dberrno = $this->errno (); 1114 Maximum Personnel connection
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;
}
}
}


http://www.bkjia.com/PHPjc/445047.html www.bkjia.com true http://www.bkjia.com/PHPjc/445047.html techarticle This is a common use of MySQL database class Oh, download down to see it. Class Mysqlclass {//define entire database access class Mysqlclass var $querynum = 0;//Number of queries variable//open ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.