MySQL database operation class (version 1127, provide source download) _php Tutorial

Source: Internet
Author: User
Tags php download
Mysql.class.php Download
Copy CodeThe code is as follows:
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
?>

http://www.bkjia.com/PHPjc/322615.html www.bkjia.com true http://www.bkjia.com/PHPjc/322615.html techarticle Mysql.class.php Download Copy code code as follows: PHP class Mysql {private $db _host;//host address private $db _user;//user name private $db _pass;// Connection Password private $db _name ...

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