PHP connection to MySQL database practical class

Source: Internet
Author: User
The code is as follows Copy Code

Class MySQL {
var $linkid =null;

function __construct ($dbhost, $dbuser, $DBPW, $dbname = ', $dbcharset = ' GBK ', $connect = 1) {
$this-> Connect ($dbhost, $dbuser, $DBPW, $dbname, $dbcharset, $connect);
}

Function Connect ($dbhost, $dbuser, $DBPW, $dbname = ', $dbcharset = ' GBK ', $connect =1) {
$func = Empty ($connect)? ' Mysql_pconnect ': ' mysql_connect ';
if (! $this->linkid = @ $func ($dbhost, $dbuser, $DBPW, True)) {
$this->dbshow (' Can not connect to mysql! ');
} else {
if ($this->dbversion () > ' 4.1 ') {
mysql_query ("Set names GBK");
if ($this->dbversion () > ' 5.0.1 ') {
mysql_query ("Set sql_mode = '", $this->linkid);
}
}
}
if ($dbname) {
if (mysql_select_db ($dbname, $this->linkid) ===false) {
$this->dbshow ("can" T select MySQL database ($dbname)!);
}
}
}

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

function query ($sql) {
if (! $query = @mysql_query ($sql, $this->linkid)) {
$this->dbshow ("Query error: $sql");
}else{
return $query;
}
}

function GetAll ($sql, $type =mysql_assoc) {
$query = $this->query ($sql);
while ($row = Mysql_fetch_array ($query, $type)) {
$rows [] = $row;
}
return $rows;
}

function GetOne ($sql, $type =mysql_assoc) {
$query = $this->query ($sql, $this->linkid);
$row = Mysql_fetch_array ($query, $type);
return $row;
}

function Fetch_array ($result, $type = Mysql_assoc) {
return mysql_fetch_array ($result);
}

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

function Num_rows () {
Return mysql_num_rows ($this->linkid);
}

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

function insert_id () {
Return mysql_insert_id ($this->linkid);
}

function Free_result () {
Return Mysql_free_result ($this->linkid);
}

Function error () {
Return mysql_error ($this->linkid);
}

function errno () {
Return Mysql_errno ($this->linkid);
}

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

function Dbversion () {
Return Mysql_get_server_info ($this->linkid);
}

function Dbshow ($msg) {
if ($msg) {
echo "Error:". $msg. " <br><br> ";
}else{
echo "errno:". $this->errno (). " <br>error: ". $this->error ();
}
Exit
}

}

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.