A PHP database connection class _php tutorial

Source: Internet
Author: User
Tags mysql tutorial php database php database connection class
This article provides two database tutorial connection examples, mainly about PHP tutorial MySQL tutorial data related operations, there is a need for friends can use to see.

Class MySQL {
Private $db _host; Host Address
Private $db _user; User name
Private $db _pass; Connection password
Private $db _name; Name
Private $db _charset; Coding
Private $conn;
Public $debug =false;//Debug switch, off by default
Private $query _id; Used to determine if the SQL statement executed successfully
Private $result; Result set
Private $num _rows; The number of rows in the result set, only valid for select
Private $insert _id; ID generated by the insert operation in the previous step
Construction/destructor
function __construct ($db _host, $db _user, $db _pass, $db _name, $db _charset, $conn) {
$this->db_host = $db _host;
$this->db_user = $db _user;
$this->db_pass = $db _pass;
$this->db_name = $db _name;
$this->db_charset = $db _charset;
$this->conn = $conn;
$this->connect ();
}
function __destruct () {
@mysql_close ($this->conn);
}
Connect/Select Database
Public Function connect () {
if ($this->conn = = ' Pconn ') {
@ $this->conn = mysql_pconnect ($this->db_host, $this->db_user, $this->db_pass);
} else {
@ $this->conn = mysql_connect ($this->db_host, $this->db_user, $this->db_pass);
}
if (! $this->conn) {
$this->show_error (' Database-Connection failed: User name or password error! ');
}
if (! @mysql_select_db ($this->db_name, $this->conn)) {
$this->show_error ("Database-Select failed: Database $this->db_name unavailable");
}
mysql_query ("Set names $this->db_charset");
return $this->conn;
}
Query method
Public Function Query ($sql) {
if ($this->query_id) $this->free_result ();
$this->query_id = @mysql_query ($sql, $this->conn);
if (! $this->query_id) $this->show_error ("SQL statement "$sql"Error encountered during execution ");
return $this->query_id;
}
Show detailed error messages
Public Function Show_error ($msg) {
if ($this->debug) {
$errinfo = Mysql_error ();
echo "Error: $msg
return: $errinfo

";
}else{
Echo '

An error has occurred!

';
}
}
Get information on whether query execution succeeded or not
Public Function Get_query_info ($info) {
if ($this->query_id) {
Echo $info;
}
}
Query all
Public function FindAll ($table _name) {
$this->query ("select * from $table _name");
}
Mysql_fetch_array
Public Function Fetch_array () {
if ($this->query_id) {
$this->result = mysql_fetch_array ($this->query_id);
return $this->result;
}
}
// ......
Public Function Fetch_assoc () {
if ($this->query_id) {
$this->result = Mysql_fetch_assoc ($this->query_id);
return $this->result;
}
}
Public Function Fetch_row () {
if ($this->query_id) {
$this->result = mysql_fetch_row ($this->query_id);
return $this->result;
}
}
Public Function Fetch_object () {
if ($this->query_id) {
$this->result = mysql_fetch_object ($this->query_id);
return $this->result;
}
}
Get Num_rows
Public Function num_rows () {
if ($this->query_id) {
$this->num_rows = mysql_num_rows ($this->query_id);
return $this->num_rows;
}
}
Get insert_id
Public Function insert_id () {
return $this->insert_id = mysql_insert_id ();
}
Show how many tables are in total
Public Function Show_tables () {
$this->query ("Show Tables");
if ($this->query_id) {
echo "Database $this->db_name common". $this->num_rows ($this->query_id). "Sheet
";
$i = 1;
while ($row = $this->fetch_array ($this->query_id)) {
echo "$i--$row [0]
";
$i + +;
}
}
}
Displays the total number of databases
Public Function Show_dbs () {
$this->query ("Show Databases");
if ($this->query_id) {
echo "Common database". $this->num_rows ($this->query_id). "A
";
$i = 1;
while ($this->row = $this->fetch_array ($this->query_id)) {
echo "$i--". $this->row[database]. "
";
$i + +;
}
}
}
Delete database: Return Delete result
Public Function drop_db ($db _name= ") {
if ($db _name = = ") {
$db _name = $this->db_name;//Delete the current database by default
$this->query ("Drop database $db _name");
}else {
$this->query ("Drop database $db _name");
}
if ($this->query_id) {
Return "Database $db _name Delete succeeded";
}else {
$this->show_error ("Database $db _name Delete failed");
}
}
Delete data table: Return Delete result
Public Function drop_table ($table _name) {
$this->query ("drop table $table _name");
if ($this->query_id) {
Return "Data sheet $table _name Delete succeeded";
}else {
$this->show_error ("Data table $table _name delete failed");
}
}
Create a database
Public Function create_db ($db _name) {
$this->query ("CREATE Database $db _name");
if ($this->query_id) {
Return "Database $db _name created successfully";
}else {
$this->show_error ("Database $db _name creation failed");
}
}
Get Database version
Public Function Get_info () {
Echo Mysql_get_server_info ();
}
Freeing memory
Public Function Free_result () {
if (@mysql_free_result ($this->query_id))
unset ($this->result);
$this->query_id = 0;
}
}//End Class
?>

The following provides an automatic selection of database remote or local connection code

Includes MySQL operation class
Include_once ' mysql.class.php ';
Local MySQL Data
$mysql _local_data = Array (' db_host ' = ' localhost ',
' Db_user ' = ' root ',
' Db_pass ' = ' root ',
' db_name ' = ' test ');
Remote MySQL Data
$mysql _remote_data = Array (' db_host ' = ' 61.183.41.178 ',
' Db_user ' = ' xxx ',
' Db_pass ' = ' xxx ',
' db_name ' = ' xxx ');
Public data
$TB _prefix = ' php95_ ';
$db _charset = ' utf-8 ';
Local connection succeeded instantiate local MySQL class, otherwise connect remote database and instantiate MySQL class
if (@mysql_connect ($mysql _local_data[db_host], $mysql _local_data[db_user], $mysql _local_data[db_pass]))
$db = new MySQL ($db _host, $mysql _local_data[db_user], $mysql _local_data[db_pass], $mysql _local_data[db_name], $db _ CharSet, $conn);
Else
$db = new MySQL ($mysql _remote_data[db_host], $mysql _remote_data[db_user], $mysql _remote_data[db_pass], $mysql _remote _data[db_name], $db _charset, $conn);
$db->show_tables (); Test: Displays all table names under the current database
?>

Suppose we want to operate the database of the virtual host in the test.php file, first of all in the local debugging, then must connect local, remote two different database, the question: How to let test.php automatically identify the current connection local or remote database?

http://www.bkjia.com/PHPjc/630771.html www.bkjia.com true http://www.bkjia.com/PHPjc/630771.html techarticle This article provides two database tutorial connection instances, mainly about PHP tutorial MySQL tutorial data related operations, the need for friends can use to see. php class MySQL {private $db _h ...

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