MySQL Database connection class

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

Class MySQL {
Public $sqlserver = ' localhost ';
Public $sqluser = ' root ';
Public $sqlpassword = ';
Public $database;

Public $last _query = ';

Private $connection;
Private $query _result;

Public Function __construct () {}

Public Function __destruct () {
$this->close ();
}

//+======================================================+
Create a connection to the MySQL database
//+======================================================+
Public Function Connect ($server = null, $user = NULL, $password = NULL, $database = null) {
if (Isset ($server)) $this->sqlserver = $server;
if (Isset ($user)) $this->sqluser = $user;
if (Isset ($password)) $this->sqlpassword = $password;
if (Isset ($database)) $this->database = $database;

$this->connection = mysql_connect ($this->sqlserver, $this->sqluser, $this->sqlpassword);
if ($this->connection) {
if (mysql_select_db ($this->database)) {
return $this->connection;
}else{
return $this->error ();
}
}else{
return $this->error ();
}
}
//+======================================================+
Execute a query
//+======================================================+
Public Function query ($query, $die = False) {
if ($query!= null) {
$this->last_query = $query;
$this->query_result = mysql_query ($query, $this->connection);
if (! $this->query_result) {
if ($die) Die ("Die:". $this->query_result);
return $this->error ();
}else{
if ($die) Die ("Die:". $this->query_result);
return $this->query_result;
}
}else{
echo "Empty query cannot be executed!";
}
}
//+======================================================+
Returns the result
//+======================================================+
Public Function GetResult () {
return $this->query_result;
}
//+======================================================+
Returns the connection
//+======================================================+
Public Function getconnection () {
return $this->connection;
}
//+======================================================+
Returns an object with properties rep
resenting the result fields Www.111cn.net and values
//+======================================================+
Public Function GetObject ($qry = null) {
if (Isset ($qry)) $this->query ($qry);
Return Mysql_fetch_object ($this->getresult ());
}
//+======================================================+
Returns an array with keys Representi
Ng the result fields and values
//+======================================================+
Public Function GetArray ($query _id = "") {
if ($query _id = = null) {
$return = mysql_fetch_array ($this->getresult ());
}else{
$return = mysql_fetch_array ($query _id);
}
Return $return? $return: $this->error ();
}
//+======================================================+
Returns the number of rows in the Res
Ult
//+======================================================+
Public Function getnumrows ($qry = null) {
if (Isset ($qry)) $this->query ($qry);
$amount = mysql_num_rows ($this->getresult ());
return empty ($amount)? 0: $amount;
}
//+======================================================+
Returns if the result contains rows
//+======================================================+
Public Function hasresults ($qry = null) {
if (Isset ($qry)) $this->query ($qry);
return $this->getnumrows ($qry) > 0;
}
//+======================================================+
Returns the number of rows that where
Affected by the last action
//+======================================================+
Public Function getaffectedrows ($qry = null, $query _id = null) {
if (Isset ($qry)) $this->query ($qry);
if (Empty ($query _id)) {
$return = Mysql_affected_rows ($this->getresult ());
}else{
$return = Mysql_affected_rows ($query _id);
}
Return $return? $return: $this->error ();
}
//+======================================================+
Returns the auto generated ID from th
E Last Insert Action
//+======================================================+
Public Function Getinsertid ($connection _link = null) {
Return mysql_insert_id (Isset ($connection _link)? $connection _link: $this->connection);
}
//+======================================================+
Close the connection to the MySQL dat
Abase
//+======================================================+
Public function Close () {
if (Isset ($this->connection)) {
Return @mysql_close ($this->connection);
}
else {
return $this->error ();
}
}
//+======================================================+
Outputs the MySQL error
//+======================================================+
Private Function error () {
if (mysql_error ()!= ') {
Echo ' <b>mysql errorwww.111cn.net</b&gt: '. Mysql_error (). ' <br/> ';
}
}
}

Demo
Database object Initialization
$db = new MySQL ();
$db->connect ("localhost", "root", "123456", "user");

Update query
$db->query ("UPDATE table_name set field_name = value where Another_field = Another_value");

Select with Check for record amount

if ($db->hasresults ("SELECT * from UserInfo")) {
Loop through the user records, and get them as objects
Note This GetObject method would use the last executed query when not provided with a new one
while ($user = $db->getobject ()) {
echo "User $user->username is called $user->password<br/>";
}
}
else {
echo "No results where found";
}

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.