PHP connection MySQL Database

Source: Internet
Author: User
Tags garbage collection table name

/*

The code is as follows Copy Code

* Created on 2010-4-21
*
* The class for control MySQL
*
* Made by S71ence
*
* @ $host
* @ $user _name
* @ $user _pwd
* @ $data _base
* @ $coding
*/
Class MySQL
{
private $host;//Host Name
Private $user _name;//User name
Private $user _pwd;//Password
Private $data _base;//Database name
private $coding;/code

constructor to initialize the operation
function __construct ($host, $user _name, $user _pwd, $data _base, $coding)
{
$this->host= $host;
$this->user_name= $user _name;
$this->user_pwd= $user _pwd;
$this->data_base= $data _base;
$this->coding= $coding;
$this->connect ();//Initialize Connection
}

/*********************************************************************************************
* Database
* Basic Method
********************************************************************************************/

Database connection
Function Connect ()
{
$link =mysql_connect ($this->host, $this->user_name, $this->user_pwd) or Die ($this->error ());
mysql_select_db ($this->data_base, $link) or die ("Unable to connect to the database". $this->data_base);
mysql_query ("Set names ' $this->coding '");
}

Error message
Function error ()
{
return Mysql_error ();
}

Mysql_query () method
function query ($sql, $type = ')
{
if (!) ( $query = mysql_query ($sql)))
{
$this->show (' say: ', $sql);
}

echo $sql. " <br/> "//Comments after test completion
return $query;
}

SQL statement Display
Function Show ($message = ', $sql = ')
{
if (! $sql)
{
Echo $message;
}
Else
{
echo $message. ' <br> '. $sql;
}
}

Mysql_affected_rows () method
function Affected_rows ()
{
return Mysql_affected_rows ();
}

Mysql_result method
function result ($query, $row)
{
Return mysql_result ($query, $row);
}

Mysql_num_rows method
function Num_rows ($query)
{
Return @mysql_num_rows ($query);
}

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

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

mysql_insert_id method
function insert_id ()
{
return mysql_insert_id ();
}

Mysql_fetch_row method
function Fetch_row ($query)
{
Return mysql_fetch_row ($query);
}

Mysql_get_server_info method
Function version ()
{
return Mysql_get_server_info ();
}

Mysql_fetch_array () method
function Fetch_array ($result)
{
return mysql_fetch_array ($result);
}

Mysql_close method
function Close ()
{
return Mysql_close ();
}


/*********************************************************************
* Database
* Functional Approach
*********************************************************************/

/*
* Insert Method
* $table Table name
* $fields Field Name
* $value field value
*/

function Fn_insert ($table, $fields, $values)
{
return $this->query (insert into $table ($fields) VALUES ($values));
$this->close ();
}


/*
* Select method
* $table Table name
* $fields Field Name
* $condition Query conditions
* $order Sorting criteria
* $limit out the number of strips
*/
function Fn_select ($table, $fields, $condition, $order, $limit)
{
$query = "Select $fields from $table";

if ($condition!= "")
{
$query. = "where $condition";
}

if ($order!= "")
{
$query. = "ORDER by $order";
}

if ($limit!= "")
{
$query. = "Limit $limit";
}

return $this->query ($query);
$this->close ();
}


/*
* Delete method
* $table Table name
* $fields Field Name
* $values field value
*/
function Fn_delete ($table, $condition)
{
return $this->query ("Delete from $table where $condition");
$this->close ();
}


/*
* Update method
* $table Table name
* $fields Field Name
* $values field value
*/
function Fn_update ($table, $set, $condition)
{
$sql = "Update $table set $set";
if ($condition!= "")
{
$sql. = "where $condition";
}

return $this->query ($sql);
$this->close ();
}


/*
* destructor, garbage collection
*/
function __destruct ()
{
echo "Clear";
}
}


Call method

The code is as follows Copy Code

$db = new MySQL (' 127.0.0.1 ', ' username ', ' password ', ' databasename ', ' UTF8 ');

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.