PHP Access MySQL Database encapsulation Class (with function description)

Source: Internet
Author: User
Tags array character set insert mysql pconnect query mysql database
Copy CodeThe code is as follows:
<?php
/*
MYSQL Database Access Encapsulation class
MYSQL data access mode, PHP4 support process access with Mysql_, PHP5 start to support mysqli_ process and mysqli object-oriented
Access mode, this encapsulation class with Mysql_ encapsulation
General flow of data access:
1, Connection database mysql_connect or Mysql_pconnect
2, select Database mysql_select_db
3, execute SQL query mysql_query
4, processing the returned data mysql_fetch_array mysql_num_rows MYSQL_FETCH_ASSOC mysql_fetch_row etc
*/
Class Db_mysql
{
var $querynum = 0; The number of times the current page process queried the database
var $dblink; Database connection Resources
Link Database
Function Connect ($dbhost, $dbuser, $DBPW, $dbname = ', $dbcharset = ' utf-8 ', $pconnect =0, $halt =true)
{
$func = Empty ($pconnect)? ' Mysql_connect ': ' Mysql_pconnect ';
$this->dblink = @ $func ($dbhost, $dbuser, $DBPW);
if ($halt &&! $this->dblink)
{
$this->halt ("Unable to link to database!") ");
}
Set query Character Set
mysql_query ("SET character_set_connection={$dbcharset},character_set_results={$dbcharset},character_set_client= Binary ", $this->dblink);
Select Database
$dbname && @mysql_select_db ($dbname, $this->dblink);
}
Select Database
function select_db ($dbname)
{
Return mysql_select_db ($dbname, $this->dblink);
}
Execute SQL query
function query ($sql)
{
$this->querynum++;
Return mysql_query ($sql, $this->dblink);
}
Returns the number of record rows affected by the most recent insert,update or delete query associated with a connection handle
function Affected_rows ()
{
Return Mysql_affected_rows ($this->dblink);
}
Gets the number of rows in the result set, only valid for the result set of the select query
function Num_rows ($result)
{
Return mysql_num_rows ($result);
}
Get the query results for a single grid
function result ($result, $row =0)
{
Return mysql_result ($result, $row);
}
Gets the ID generated by the previous INSERT operation, only for operations where the Auto_increment ID is available for the table
function insert_id ()
{
return ($id = mysql_insert_id ($this->dblink)) >= 0? $id: $this->result ($this->query ("Select last_insert_id ()"), 0);
}
Extracts the current row from the result set, and returns the form of an associative array represented by a number key
function Fetch_row ($result)
{
Return mysql_fetch_row ($result);
}
Extracts the current row from the result set, returning the form as an associative array of the field name key
function Fetch_assoc ($result)
{
Return Mysql_fetch_assoc ($result);
}
Extracts the current row from the result set, returning in the form of an associative array of field names and numbers as key
function Fetch_array ($result)
{
return mysql_fetch_array ($result);
}
Close link
function Close ()
{
Return Mysql_close ($this->dblink);
}
Output a simple error HTML message and terminate the program
function Halt ($msg)
{
$message = "$message. = "<meta content= ' text/html;charset=gb2312 ' >\n";
$message. = "$message. = "<body>\n";
$message. = "Database error:". Htmlspecialchars ($msg). \ n ";
$message. = "</body>\n";
$message. = "Echo $message;
Exit;
}
}
?>


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.