MySQL database operation class (1127 version, provide source download)
Last Update:2017-02-28
Source: Internet
Author: User
Mysql.class.php Download
Copy Code The code is as follows:
<?php
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, default shutdown
Private $query _id; Used to determine whether the SQL statement was executed successfully
Private $result; Result set
Private $num _rows; Number of rows in the result set, valid only for select
Private $insert _id; ID generated by the previous INSERT operation
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 <b>\" $sql \ "error encountered while executing");
return $this->query_id;
}
Show verbose error messages
Public Function Show_error ($msg) {
if ($this->debug) {
$errinfo = Mysql_error ();
echo "Error: $msg <br/> return: $errinfo <p>";
}else{
Echo ' <p> there was an error! <p> ';
}
}
Get information about the success of query execution
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 <br/>";
$i = 1;
while ($row = $this->fetch_array ($this->query_id)) {
echo "$i--$row [0]<br/>";
$i + +;
}
}
}
Show how many databases are in total
Public Function Show_dbs () {
$this->query ("Show Databases");
if ($this->query_id) {
echo "Shared database". $this->num_rows ($this->query_id). "<br/>";
$i = 1;
while ($this->row = $this->fetch_array ($this->query_id)) {
echo "$i--". $this->row[database]. " <br/> ";
$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 "Datasheet $table _name Delete succeeded";
}else {
$this->show_error ("datasheet $table _name deletion failed");
}
}
Creating 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 ();
}
Free memory
Public Function Free_result () {
if (@mysql_free_result ($this->query_id))
unset ($this->result);
$this->query_id = 0;
}
}//End Class
?>