MySQL database operation class _php tutorial

Source: Internet
Author: User
Tags mysql query mysql tutorial pconnect php mysql
MySQL database operation class This is the use of PHP MySQL database operation class function, he can delete, modify, add, backup and other operations on the database Oh, but also support two database connection method Oh, the previous database encoding and other settings.

MySQL Tutorial database Tutorial Action Class
This paragraph is the use of PHP tutorial MySQL database operation class function, he can delete, modify, add, backup and other operations on the database Oh, but also support two kinds of database connection method Oh, the previous database encoding and other settings.
*/

Class Dbstuff {
var $querynum = 0;
var $link;
var $sqlid;
var $record;
var $dbcharset = ' Utf-8 ';
var $charset = ' Utf-8 ';
Function Connect ($dbhost, $dbuser, $DBPW, $dbname = ", $pconnect = 0, $halt = True) {
if ($pconnect) {
if (! $this->link = @mysql_pconnect ($dbhost, $dbuser, $DBPW)) {
$halt && $this->halt (' Can not connect to MySQL server ');
}
} else {
if (! $this->link = @mysql_connect ($dbhost, $dbuser, $DBPW, 1)) {
$halt && $this->halt (' Can not connect to MySQL server ');
}
}

if ($this->version () > ' 4.1 ') {
Global $charset, $dbcharset;
if (! $dbcharset && In_array (Strtolower ($charset), Array (' GBK ', ' Big5 ', ' utf-8 ')) {
$dbcharset = Str_replace ('-', ' ', $charset);
}

if ($dbcharset) {
@mysql_query ("Set character_set_connection= $dbcharset, character_set_results= $dbcharset, character_set_client= Binary ", $this->link);
}

if ($this->version () > ' 5.0.1 ') {
@mysql_query ("Set sql_mode=", $this->link);
}
}

if ($dbname) {
@mysql_select_db ($dbname, $this->link);
}

}

function select_db ($dbname) {
Return mysql_select_db ($dbname, $this->link);
}

function Fetch_array ($query, $result _type = Mysql_assoc) {
Return mysql_fetch_array ($query, $result _type);
}

function query ($sql, $type = ") {
Global $debug, $discuz _starttime, $sqldebug, $sqlspenttimes;

$func = $type = = ' unbuffered ' && @function_exists (' Mysql_unbuffered_query ')?
' Mysql_unbuffered_query ': ' mysql_query ';
if (! ( $query = $func ($sql, $this->link))) {
if (In_array ($this->errno (), Array (2006)) && substr ($type, 0, 5)! = ' Retry ') {
$this->close ();
$this->connect ($dbhost, $dbuser, $DBPW, $dbname, $pconnect);
$this->query ($sql, ' Retry '. $type);
} elseif ($type! = ' silent ' && substr ($type, 5)! = ' silent ') {
$this->halt (' MySQL query error ', $sql);
}
}

$this->querynum++;
$this->sqlid= $query;
return $query;
}

function Affected_rows () {
Return Mysql_affected_rows ($this->link);
}

Function error () {
Return ($this->link) mysql_error ($this->link): Mysql_error ());
}

function errno () {
Return Intval ($this->link) Mysql_errno ($this->link): Mysql_errno ());
}

function result ($query, $row) {
$query = @mysql_result ($query, $row);
return $query;
}

function Result_first ($sql) {
return $this->result ($this->query ($sql), 0);
}

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

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

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

function insert_id () {
return ($id = mysql_insert_id ($this->link)) >= 0? $id: $this->result ($this->query ("Select last_insert_id ()"), 0);
}

function Fetch_row ($query) {
$query = Mysql_fetch_row ($query);
return $query;
}

function Fetch_fields ($query) {
Return Mysql_fetch_field ($query);
}

Function version () {
Return Mysql_get_server_info ($this->link);
}

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

function nr ($sql _id= "") {
if (! $sql _id) $sql _id= $this->sqlid;
Return mysql_num_rows ($sql _id);
}

function NF ($sql _id= "") {
if (! $sql _id) $sql _id= $this->sqlid;
Return Mysql_num_fields ($sql _id);
}

function NextRecord ($sql _id= "") {
if (! $sql _id) $sql _id= $this->sqlid;
if ($this->record=mysql_fetch_array ($sql _id)) return $this->record;
else return false;
}

function f ($name) {
if ($this->record[$name]) return $this->record[$name];
else return false;
}

function Lock ($tblname, $op = "Write") {
if (mysql_query ("Lock Tables". $tblname. " ". $op)) return true;
else return false;
}

function unlock ()
{if (mysql_query ("Unlock tables)") return true; else return false;}

function Ar () {
Return @mysql_affected_rows ($this->link);
}

function i_id () {
return mysql_insert_id ();
}

function Halt ($message = ', $sql = ') {
Define (' Cache_forbidden ', true);
Include (' db_mysql_error.inc.php ');
}
}

?>

http://www.bkjia.com/PHPjc/630795.html www.bkjia.com true http://www.bkjia.com/PHPjc/630795.html techarticle MySQL database operation class This is the use of PHP MySQL database operation class function, he can delete, modify, add, backup and other operations on the database Oh, but also support two database connection ...

  • 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.