A common PHP MySQL database connection class _php tutorial

Source: Internet
Author: User
Tags php mysql
This is a very basic thing we can through the constructor to implement the creation of the class is automatically connected to the MySQL server, as long as we set the $name, $pass, $table the value of three variables is good.
The code is as follows Copy Code

Class connectionmysql{
Host
Private $host = "localhost";
Username of the database
Private $name = "root";
Password of the database
Private $pass = "";
Database name
Private $table = "phptest";
Encoded form
Private $ut = "Utf-8";


constructor function
function __construct () {
$this->ut= $ut;
$this->connect ();

}

Links to databases
function Connect () {
$link =mysql_connect ($this->host, $this->name, $this->pass) or Die ($this->error ());
mysql_select_db ($this->table, $link) or Die ("No Database:". $this->table);
mysql_query ("SET NAMES ' $this->ut '");
}

function query ($sql, $type = ") {
if (! ( $query = mysql_query ($sql)) $this->show (' Say: ', $sql);
return $query;
}

Function Show ($message = ', $sql = ') {
if (! $sql) echo $message;
else echo $message. '
'. $sql;
}

function Affected_rows () {
return Mysql_affected_rows ();
}

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

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

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

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

function insert_id () {
return mysql_insert_id ();
}

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

Function version () {
return Mysql_get_server_info ();
}

function Close () {
return Mysql_close ();
}

Inserting values into the $table table
function Fn_insert ($table, $name, $value) {
$this->query ("INSERT into $table ($name) value ($value)");
}
Deletes a record in table $table based on the $id value
function Fn_delete ($table, $id, $value) {
$this->query ("Delete from $table where $id = $value");
echo "ID is". $id. "The record was successfully deleted!";
}
}

Calling methods

$db = new Connectionmysql ();

$db->fn_insert (' Test ', ' id,name,sex ', ' ' ', ' hongtenzone ', ' M ');
$db->fn_delete (' Test ', ' id ', 1);

?>

Here I'm going to tell you about constructors

The code is as follows Copy Code
constructor function
function __construct () {
$this->ut= $ut;
$this->connect ();

}


This page uses the constructor specifically to do not call the database connection class in the function, otherwise the current page will have multiple connections if access to a large server will be out of MySQL has gone of the formulation oh.

http://www.bkjia.com/PHPjc/632934.html www.bkjia.com true http://www.bkjia.com/PHPjc/632934.html techarticle This is a very basic thing we can through the constructor to implement the creation of the class is automatically connected to the MySQL server, as long as we set the $name, $pass, $table the value of three variables is good. to be a substitute for ...

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