PHP Access MySQL Package

Source: Internet
Author: User
Tags pconnect

<?php
/*
MYSQL Database Access wrapper class
MYSQL data access mode, PHP4 supports process access starting with Mysql_, PHP5 start supporting processes that begin with Mysqli_ and mysqli object-oriented
Access mode, this package class is packaged in Mysql_

General flow of data access:
1, connecting 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; Number of times the current page process queries 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 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 the connection handle
function Affected_rows ()
{
Return Mysql_affected_rows ($this->dblink);
}

Gets the number of rows in the result set, valid only for the result set of the select query
function Num_rows ($result)
{
Return mysql_num_rows ($result);
}

Get results for single-cell queries
function result ($result, $row =0)
{
Return mysql_result ($result, $row);
}

Get the ID generated by the INSERT operation in the previous step, only valid for operations with Auto_increment ID 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, returned as an associative array of number key
function Fetch_row ($result)
{
Return mysql_fetch_row ($result);
}

Extracts the current row from the result set, returning as an associative array with the field name key
function Fetch_assoc ($result)
{
Return Mysql_fetch_assoc ($result);
}

Extracts the current row from the result set, returned as an associative array of field names and numbers
function Fetch_array ($result)
{
return mysql_fetch_array ($result);
}

Close link
function Close ()
{
Return Mysql_close ($this->dblink);
}

Output simple error HTML hint information 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.