PHP implementation of MySQL Backup recovery method _php technique

Source: Internet
Author: User
Tags explode mkdir mysql version mysql connect php mysql mysql backup

This article describes the PHP implementation of the MySQL backup recovery method of the volume processing. Share to everyone for your reference. The specific analysis is as follows:

Processing is the grasp of the data to be processed into a small file for processing, here I would like to introduce a PHP MySQL backup recovery processing class, to achieve MySQL database backup, select the table for backup, to achieve a single SQL file and the volume of SQL import.

Sub-volume import class and the idea of detailed

Database import Export is a necessary feature in the background, a search on the internet, there are many about the database import export, but basically a large system, including many we do not need, and they are the form of their own backstage, I do not like to take people's things into their own backstage, I need is their own things, So the reference to a lot, I wrote a about the introduction of the volume of the class to facilitate the call, welcome everyone to Pat Bricks.

Here for the volume file is the ' _v1.sql ' end, the implementation of a single SQL file and the volume of SQL import, the split-volume import can choose whether the current volume import the remaining volumes, we need to directly call the class to complete.

Host, user name, password, database name, database code, respectively

Copy Code code as follows:
$db = new Datamanage (' localhost ', ' root ', ' root ', ' test ', ' UTF8 ');

SQL file, whether to import only a single SQL (that is, if there are other volumes that are not imported).

Copy Code code as follows:
$db->restore ('./backup/20120516211738_all_v1.sql ', false);



corresponding to how to list the backup of the SQL file or choose SQL, and so on, the implementation of their own, that is not in this category, but also very simple.

There are currently only implemented database import, about the database export, writing function, the following is the complete class code, concrete ideas and implementation of the code inside all have instructions, here is not to repeat, the code is as follows:

Copy Code code as follows:
<?php


/**


* @author Yanue


* Description: The _v1.sql is the end of the volume file


* Function: Implement a single SQL file and the volume of SQL import, the split-volume import can choose whether the current volume to import the remaining volumes


* Use method:


*


*


* ------------------------------------------------------------------


Host, user name, password, database name, database code, respectively


$db = new Datamanage (' localhost ', ' root ', ' root ', ' test ', ' UTF8 ');


SQL file, whether to import only single SQL (that is, if there are other volumes that are not imported)


$db->restore ('./backup/20120516211738_all_v1.sql ', false);


*----------------------------------------------------------------------


*/


Class Datamanage {


var $db; Database connection


var $database; The database used


var $sqldir; Database backup folder





/**


* Initialization


*


* @param string $host


* @param string $username


* @param string $password


* @param string $database


* @param string $charset


*/


function __construct ($host = ' localhost ', $username = ' root ', $password = ', $database = ' Test ', $charset = ' utf8 ') {


$this->host = $host;


$this->username = $username;


$this->password = $password;


$this->database = $database;


$this->charset = $charset;


Connecting to a database


$this->db = mysql_connect ($this->host, $this->username, $this->password) or Die ("database connection failed.");


Choose which database to use


mysql_select_db ($this->database, $this->db) or Die ("Cannot open database");


How the database is encoded


mysql_query (' SET NAMES '. $this->charset, $this->db);


}





/**


* Import Backup Data


* Description: Volume file format 20120516211738_all_v1.sql


*


* @param string $sqlfile


* @param bool $single


*/


function Restore ($sqlfile, $single = FALSE) {


Detect if a file exists


if (! file_exists ($sqlfile)) {


Exit ("file does not exist!") Please check ");


}


$this->lock ($this->database);


Get Database storage location


$sqlpath = PathInfo ($sqlfile);


$this->sqldir = $sqlpath [' dirname '];


Detects if a volume is included, separates similar 20120516211738_all_v1.sql from _v, and indicates a split volume


$volume = Explode ("_v", $sqlfile);


$volume _path = $volume [0];


echo "Do not refresh and close the browser to prevent the program from being aborted, if careless!" will cause the database structure to be damaged <br/> ";


ECHO is importing backup data, please wait a moment! <br/> ";


if (Emptyempty ($volume [1]) | | $single) {


echo "is importing Sql:<span style= ' color: #f00; ' > ". $sqlfile. ' </span><br/> ';


No sub-volume


if ($this->_import ($sqlfile)) {


echo "Database import succeeded!" ";


} else {


Exit (' Database import failed! ' );


}


} else {


A volume is present, the current is the first few volumes, and the remaining volumes are cycled


$volume _id = Explode (". Sq", $volume [1]);


Current sub-volume is $volume_id


$volume _id = intval ($volume _id [0]);


while ($volume _id) {


$tmpfile = $volume _path. "_v". $volume _id. ". SQL";


Additional volumes exist, continue execution


if (file_exists ($tmpfile)) {


To perform an import method


ECHO is importing the volume $volume_id:<span style= ' color: #f00; ' > ". $tmpfile. ' </span><br/> ';


if ($this->_import ($tmpfile)) {





} else {


Exit ("Import volume $volume_id:<span style= ' color: #f00; ' > ". $tmpfile. ' </span> failed! The database structure may be corrupted! Please try to import ' from Volume 1 ');


}


} else {


echo "This volume backup was successfully imported!" <br/> ";


Return


}


$volume _id + +;


}


}


}





/**


* Import SQL into the database (normal import)


*


* @param string $sqlfile


* @return Boolean


*/


Private Function _import ($sqlfile) {


$name = basename ($sqlfile);


$sqls = file ($sqlfile);


foreach ($sqls as $sql) {


Str_replace ("R", "", $sql);


Str_replace ("n", "", $sql);


if (! mysql_query (Trim ($sql), $this->db))


return false;


}


return true;


}





To close a database connection


Private Function Close () {


Mysql_close ($this->db);


}





Lock the database to prevent errors during backup or import


Private function Lock ($tablename, $op = "WRITE") {


if (mysql_query ("Lock Tables"). $tablename. " " . $OP))


return true;


Else


return false;


}





Unlock


Private function Unlock () {


if (mysql_query ("Unlock tables")


return true;


Else


return false;


}





destructor


function __destruct () {


mysql_query ("Unlock Tables", $this->db);


Mysql_close ($this->db);


}


}


?>



MySQL backup restores the volume processing, the invocation is simple.

Sub-volume Import ideas:

Read the SQL file by row, save each row as a complete SQL statement to an array and then loop through the Insert database. But it took me a long time to create a table statement with more than one line, which needs to be handled separately. Feel the article is very long ah, mainly that class file to occupy.

Update Description:

1. Eliminate SQL Import when excluding the comments in the SQL file '-' so that the single double quotes in SQL can not be imported

2. Direct-line read SQL execution, avoid the combination of SQL statements into the array and then read the import SQL from the array, improve efficiency.

Download Address: Https://github.com/yanue/Dbmanage

The exported SQL file format is as follows:

Copy Code code as follows:
--
--MySQL database dump
--Created by Dbmanage class and power by Yanue.
--
--Host: localhost
--Date Created: October 06, 2012 22:32
--MySQL version: 5.1.50-community
--PHP version: 5.3.9-zs5.6.0

--
--Database: ' Test '
--

-- -------------------------------------------------------

--
--Table Structure AA
--

DROP TABLE IF EXISTS ' AA ';
CREATE TABLE ' AA ' (
' ID ' int (a) unsigned not NULL auto_increment,
' Content ' text not NULL,
PRIMARY KEY (' id ')
) Engine=innodb auto_increment=2 DEFAULT Charset=utf8;

--
--the data in the Dump table AA
--

INSERT into ' AA ' VALUES (' 1 ', ' <p id= "test" ><span class= ' hahh ' style= ' line-height ":;" > I am testing data hehe </span></p> ');



The following is the class code:


Copy Code code as follows:
&lt;?php


/**


* @author Yanue


* @copyright Copyright (c) yanue.net


* @version 1.1


* Date Created: May 21, 2012





Update Time: October 6, 2012


Update Note: 1. Eliminate SQL Import by excluding annotations in SQL Files '--' to resolve SQL single double quotes cannot be imported


2. Single-row read SQL direct execution, avoid the combination of SQL statements into the array and then read the import SQL from the array, improve efficiency





* Description: The volume file is _v1.sql (20120522021241_all_v1.sql)


* Function: To achieve the MySQL database of the volume backup, select the table for backup, implementation of a single SQL file and the volume of SQL import


* Use method:


*


*------1. Database backup (Export)------------------------------------------------------------


Host, user name, password, database name, database code, respectively


$db = new Dbmanage (' localhost ', ' root ', ' root ', ' test ', ' UTF8 ');


Parameters: Which table to back up (optional), backup directory (optional, default to Backup), volume size (optional, default 2000, or 2M)


$db-&gt;backup ();


*------2. Database recovery (Import)------------------------------------------------------------


Host, user name, password, database name, database code, respectively


$db = new Dbmanage (' localhost ', ' root ', ' root ', ' test ', ' UTF8 ');


Parameters: SQL file


$db-&gt;restore ('./backup/20120516211738_all_v1.sql ');


*----------------------------------------------------------------------


*/


Class Dbmanage {


var $db; Database connection


var $database; The database used


var $sqldir; Database backup folder


Line feed


Private $ds = "n";


variables that store SQL


Public $sqlContent = "";


The end of each SQL statement


Public $sqlEnd = ";";





/**


* Initialization


*


* @param string $host


* @param string $username


* @param string $password


* @param string $database


* @param string $charset


*/


function __construct ($host = ' localhost ', $username = ' root ', $password = ', $database = ' Test ', $charset = ' utf8 ') {


$this-&gt;host = $host;


$this-&gt;username = $username;


$this-&gt;password = $password;


$this-&gt;database = $database;


$this-&gt;charset = $charset;


Set_time_limit (0);//No time limit


@ob_end_flush ();


Connecting to a database


$this-&gt;db = @mysql_connect ($this-&gt;host, $this-&gt;username, $this-&gt;password) or Die (' &lt;p class= ' Dbdebug "&G") T;&lt;span class= "Err" &gt;mysql Connect Error: &lt;/span&gt; '. Mysql_error (). ' &lt;/p&gt; ');


Choose which database to use


mysql_select_db ($this-&gt;database, $this-&gt;db) or Die (' &lt;p class= "dbdebug" &gt;&lt;span class= "Err" &gt;mysql Connect error:&lt;/span&gt; '. Mysql_error (). ' &lt;/p&gt; ');


How the database is encoded


mysql_query (' SET NAMES '. $this-&gt;charset, $this-&gt;db);





}





/*


* New Query database table


*/


function Gettables () {


$res = mysql_query ("show TABLES");


$tables = Array ();


while ($row = Mysql_fetch_array ($res)) {


$tables [] = $row [0];


}


return $tables;


}





/*


*


*------------------------------------------Database backup start----------------------------------------------------------


*/





/**


* Database backup


* Parameters: Which table to back up (optional), backup directory (optional, default to Backup), volume size (optional, default 2000, that is, 2M)


*


* @param $string $dir


* @param int $size


* @param $string $tablename


*/


function backup ($tablename = ', $dir, $size) {


$dir = $dir? $dir: './backup/';


Create a table of contents


if (! Is_dir ($dir)) {


mkdir ($dir, 0777, True) or Die (' Create folder failed ');


}


$size = $size? $size: 2048;


$sql = ';


Back up only a table


if (! Emptyempty ($tablename)) {


if (@mysql_num_rows ("show TABLES like", $tablename. "mysql_query") = = 1) {


} else {


$this-&gt;_showmsg (' Table-&lt;b&gt; '. $tablename. &lt;/b&gt;-does not exist, please check! ', true);


Die ();


}


$this-&gt;_showmsg (' Backing up the table &lt;span class= ' imp ' &gt; '. $tablename. ' &lt;/span&gt; ');


Insert Dump Information


$sql = $this-&gt;_retrieve ();


Insert Table structure Information


$sql. = $this-&gt;_insert_table_structure ($tablename);


Inserting data


$data = mysql_query ("SELECT * from".) $tablename);


Previous section of file name


$filename = Date (' Ymdhis '). "_" . $tablename;


Number of fields


$num _fields = Mysql_num_fields ($data);


First few volumes


$p = 1;


Loop each record


while ($record = Mysql_fetch_array ($data)) {


Single record


$sql. = $this-&gt;_insert_record ($tablename, $num _fields, $record);


Write file if it is greater than the size of the split volume


if (strlen ($sql) &gt;= $size * 1024) {


$file = $filename. "_v". $p. ". SQL";


if ($this-&gt;_write_file ($sql, $file, $dir)) {


$this-&gt;_showmsg ("Table-&lt;b&gt;". $tablename. "&lt;/b&gt;-volume-&lt;b&gt;". $p. "&lt;/b&gt;-data backup complete, backup file [&lt;span class= ' imp ' &gt;". $dir. $file. " &lt;/span&gt;] ");


} else {


$this-&gt;_showmsg ("Backup Table-&lt;b&gt;". $tablename. "&lt;/b&gt;-failed", true);


return false;


}


Next Sub-volume


$p + +;


Reset the $sql variable to be empty, recalculate the variable size


$sql = "";


}


}


Clean up data in time


Unset ($data, $record);


SQL size is not large enough to be divided into volumes


if ($sql!= "") {


$filename. = "_v". $p. ". SQL";


if ($this-&gt;_write_file ($sql, $filename, $dir)) {


$this-&gt;_showmsg ("Table-&lt;b&gt;". $tablename. "&lt;/b&gt;-volume-&lt;b&gt;". $p. "&lt;/b&gt;-data backup complete, backup file [&lt;span class= ' imp ' &gt;". $dir. $filename. " &lt;/span&gt;] ");


} else {


$this-&gt;_showmsg ("Backup Volume-&lt;b&gt;". $p. "&lt;/b&gt;-failure &lt;br/&gt;");


return false;


}


}


$this-&gt;_showmsg ("Congratulations!") &lt;span class= ' imp ' &gt; Backup Success &lt;/span&gt; ');


} else {


$this-&gt;_showmsg (' being backed up ');


Back up all Tables


if ($tables = mysql_query ("Show Table status from".) $this-&gt;database)) {


$this-&gt;_showmsg ("read the database structure successfully!") ");


} else {


$this-&gt;_showmsg ("Failed to read the database structure!) ");


Exit (0);


}


Insert Dump Information


$sql. = $this-&gt;_retrieve ();


Previous section of file name


$filename = Date (' Ymdhis '). "_all";


Find All Tables


$tables = mysql_query (' Show Tables ');


First few volumes


$p = 1;


Loop All Tables


while ($table = Mysql_fetch_array ($tables)) {


Get table name


$tablename = $table [0];


Get table structure


$sql. = $this-&gt;_insert_table_structure ($tablename);


$data = mysql_query ("SELECT * from".) $tablename);


$num _fields = Mysql_num_fields ($data);





Loop each record


while ($record = Mysql_fetch_array ($data)) {


Single record


$sql. = $this-&gt;_insert_record ($tablename, $num _fields, $record);


Write file if it is greater than the size of the split volume


if (strlen ($sql) &gt;= $size * 1000) {





$file = $filename. "_v". $p. ". SQL";


Write to File


if ($this-&gt;_write_file ($sql, $file, $dir)) {


$this-&gt;_showmsg ("-Volume-&lt;b&gt;". $p. "&lt;/b&gt;-data backup complete, backup file [&lt;span class= ' imp ' &gt;". $dir. $file. " &lt;/span&gt;] ");


} else {


$this-&gt;_showmsg ("Volume-&lt;b&gt;". $p. "&lt;/b&gt;-Backup Failed!", true);


return false;


}


Next Sub-volume


$p + +;


Reset the $sql variable to be empty, recalculate the variable size


$sql = "";


}


}


}


SQL size is not large enough to be divided into volumes


if ($sql!= "") {


$filename. = "_v". $p. ". SQL";


if ($this-&gt;_write_file ($sql, $filename, $dir)) {


$this-&gt;_showmsg ("-Volume-&lt;b&gt;". $p. "&lt;/b&gt;-data backup complete, backup file [&lt;span class= ' imp ' &gt;". $dir. $filename. " &lt;/span&gt;] ");


} else {


$this-&gt;_showmsg ("Volume-&lt;b&gt;". $p. "&lt;/b&gt;-Backup Failed", true);


return false;


}


}


$this-&gt;_showmsg ("Congratulations!") &lt;span class= ' imp ' &gt; Backup Success &lt;/span&gt; ');


}


}





Timely output information


Private Function _showmsg ($msg, $err =false) {


$err = $err? "&lt;span class= ' err ' &gt;ERROR:&lt;/span&gt;": ';


echo "&lt;p class= ' Dbdebug ' &gt;". $err. $msg. " &lt;/p&gt; ";


Flush ();





}





/**


* Insert Database backup base information


*


* @return String


*/


Private Function _retrieve () {


$value = ';


$value. = '--'. $this-&gt;ds;


$value. = '--MySQL database dump '. $this-&gt;ds;


$value. = '-Created by Dbmanage class, power by Yanue. ' . $this-&gt;ds;


$value. = '-http://yanue.net '. $this-&gt;ds;


$value. = '--'. $this-&gt;ds;


$value. = '--Host: '. $this-&gt;host. $this-&gt;ds;


$value. = '--Date generated: '. Date (' Y '). ' Year '. Date (' m '). ' Month '. Date (' d '). ' Day '. Date (' H:i '). $this-&gt;ds;


$value. = '--MySQL version: '. Mysql_get_server_info (). $this-&gt;ds;


$value. = '--PHP version: '. Phpversion (). $this-&gt;ds;


$value. = $this-&gt;ds;


$value. = '--'. $this-&gt;ds;


$value. = '--database: '. $this-&gt;database. '`' . $this-&gt;ds;


$value. = '--'. $this-&gt;ds. $this-&gt;ds;


$value. = '---------------------------------------------------------';


$value. = $this-&gt;ds. $this-&gt;ds;


return $value;


}





/**


* INSERT Table structure


*


* @param unknown_type $table


* @return String


*/


Private Function _insert_table_structure ($table) {


$sql = ';


$sql. = "--". $this-&gt;ds;


$sql. = "--the structure of a table." $table. $this-&gt;ds;


$sql. = "--". $this-&gt;ds. $this-&gt;ds;





Delete table if present


$sql. = "DROP TABLE IF EXISTS '". $table. '`' . $this-&gt;sqlend. $this-&gt;ds;


Get detailed table information


$res = mysql_query (' Show CREATE TABLE '. $table. '`' );


$row = Mysql_fetch_array ($res);


$sql. = $row [1];


$sql. = $this-&gt;sqlend. $this-&gt;ds;


Plus


$sql. = $this-&gt;ds;


$sql. = "--". $this-&gt;ds;


$sql. = "--dump data in a table." $table. $this-&gt;ds;


$sql. = "--". $this-&gt;ds;


$sql. = $this-&gt;ds;


return $sql;


}





/**


* Insert a single record


*


* @param string $table


* @param int $num _fields


* @param array $record


* @return String


*/


Private Function _insert_record ($table, $num _fields, $record) {


SQL Field comma split


$insert = ';


$comma = "";


$insert. = "INSERT INTO". $table. "' VALUES (";


Loop below each sub-paragraph


for ($i = 0; $i &lt; $num _fields; $i + +) {


$insert. = ($comma. "'" . Mysql_escape_string ($record [$i]). "'");


$comma = ",";


}


$insert. = ");". $this-&gt;ds;


return $insert;


}





/**


* Write to File


*


* @param string $sql


* @param string $filename


* @param string $dir


* @return Boolean


*/


Private Function _write_file ($sql, $filename, $dir) {


$dir = $dir? $dir: './backup/';


Create a table of contents


if (! Is_dir ($dir)) {


mkdir ($dir, 0777, true);


}


$re = true;


if (! @ $fp = fopen ($dir. $filename, "w+")) {


$re = false;


$this-&gt;_showmsg ("Open SQL file failed!") ", true);


}


if (! @fwrite ($FP, $sql)) {


$re = false;


$this-&gt;_showmsg ("Write SQL file failed, please file is writable", true);


}


if (! @fclose ($fp)) {


$re = false;


$this-&gt;_showmsg ("Shutdown SQL file failed!") ", true);


}


return $re;


}





/*


*


*-------------------------------: Database export-----------Split line----------: Database import--------------------------------


*/





/**


* Import Backup Data


* Description: Volume file format 20120516211738_all_v1.sql


* Parameter: File path (required)


*


* @param string $sqlfile


*/


function Restore ($sqlfile) {


Detect if a file exists


if (! file_exists ($sqlfile)) {


$this-&gt;_showmsg ("SQL file does not exist!") Please check ", true";


Exit ();


}


$this-&gt;lock ($this-&gt;database);


Get Database storage location


$sqlpath = PathInfo ($sqlfile);


$this-&gt;sqldir = $sqlpath [' dirname '];


Detects if a volume is included, separates similar 20120516211738_all_v1.sql from _v, and indicates a split volume


$volume = Explode ("_v", $sqlfile);


$volume _path = $volume [0];


$this-&gt;_showmsg ("Do not refresh and close the browser to prevent the program from being aborted, if careless!") will cause damage to the database structure ");


$this-&gt;_showmsg ("You are importing backup data, wait a minute!") ");


if (Emptyempty ($volume [1])) {


$this-&gt;_showmsg ("Importing Sql:&lt;span class= ' imp ' &gt;". $sqlfile. ' &lt;/span&gt; ');


No sub-volume


if ($this-&gt;_import ($sqlfile)) {


$this-&gt;_showmsg ("Database import succeeded!") ");


} else {


$this-&gt;_showmsg (' Database import failed! ', true);


Exit ();


}


} else {


A volume is present, the current is the first few volumes, and the remaining volumes are cycled


$volume _id = Explode (". Sq", $volume [1]);


Current sub-volume is $volume_id


$volume _id = intval ($volume _id [0]);


while ($volume _id) {


$tmpfile = $volume _path. "_v". $volume _id. ". SQL";


Additional volumes exist, continue execution


if (file_exists ($tmpfile)) {


To perform an import method


$this-&gt;msg. = "Importing a volume $volume _id: &lt;span style= ' color: #f00; ' &gt; ". $tmpfile. ' &lt;/span&gt;&lt;br/&gt; ';


if ($this-&gt;_import ($tmpfile)) {





} else {


$volume _id = $volume _id? $volume _id:1;


Exit ("Import Volume: &lt;span style= ' color: #f00; ' &gt; ". $tmpfile. ' &lt;/span&gt; failed! The database structure may be corrupted! Please try to import ' from Volume 1 ');


}


} else {


$this-&gt;msg. = "This volume backup was successfully imported!" &lt;br/&gt; ";


Return


}


$volume _id + +;


}


}if (Emptyempty ($volume [1])) {


$this-&gt;_showmsg ("Importing Sql:&lt;span class= ' imp ' &gt;". $sqlfile. ' &lt;/span&gt; ');


No sub-volume


if ($this-&gt;_import ($sqlfile)) {


$this-&gt;_showmsg ("Database import succeeded!") ");


} else {


$this-&gt;_showmsg (' Database import failed! ', true);


Exit ();


}


} else {


A volume is present, the current is the first few volumes, and the remaining volumes are cycled


$volume _id = Explode (". Sq", $volume [1]);


Current sub-volume is $volume_id


$volume _id = intval ($volume _id [0]);


while ($volume _id) {


$tmpfile = $volume _path. "_v". $volume _id. ". SQL";


Additional volumes exist, continue execution


if (file_exists ($tmpfile)) {


To perform an import method


$this-&gt;msg. = "Importing a volume $volume _id: &lt;span style= ' color: #f00; ' &gt; ". $tmpfile. ' &lt;/span&gt;&lt;br/&gt; ';


if ($this-&gt;_import ($tmpfile)) {





} else {


$volume _id = $volume _id? $volume _id:1;


Exit ("Import Volume: &lt;span style= ' color: #f00; ' &gt; ". $tmpfile. ' &lt;/span&gt; failed! The database structure may be corrupted! Please try to import ' from Volume 1 ');


}


} else {


$this-&gt;msg. = "This volume backup was successfully imported!" &lt;br/&gt; ";


Return


}


$volume _id + +;


}


}


}





/**


* Import SQL into the database (normal import)


*


* @param string $sqlfile


* @return Boolean


*/


Private Function _import ($sqlfile) {


SQL file contains an array of SQL statements


$sqls = Array ();


$f = fopen ($sqlfile, "RB");


Create a table buffer variable


$create _table = ';


while (! feof ($f)) {


Read each row of SQL


$line = fgets ($f);


This step in order to create a table to synthesize the complete SQL statement


If the end does not contain '; ' (That is, a complete SQL statement, here is the INSERT statement) and does not contain ' engine= ' (that is, the last sentence of the creation table)


if (! Preg_match ('/;/', $line) | | | preg_match ('/engine=/', $line)) {


Save this SQL statement with creating a table SQL connection


$create _table. = $line;


If you include the last sentence for creating a table


if (Preg_match ('/engine=/', $create _table)) {


Execute SQL statement Creation table


$this-&gt;_insert_into ($create _table);


Empty the current and prepare for the next table creation


$create _table = ';


}


Skip this time


Continue


}


Execute SQL statement


$this-&gt;_insert_into ($line);


}


Fclose ($f);


return true;


}





Insert a single SQL statement


Private Function _insert_into ($sql) {


if (! mysql_query (Trim ($sql))) {


$this-&gt;msg. = Mysql_error ();


return false;


}


}





/*


*-------------------------------Database Import End---------------------------------


*/





To close a database connection


Private Function Close () {


Mysql_close ($this-&gt;db);


}





Lock the database to prevent errors during backup or import


Private function Lock ($tablename, $op = "WRITE") {


if (mysql_query ("Lock Tables"). $tablename. " " . $OP))


return true;


Else


return false;


}





Unlock


Private function Unlock () {


if (mysql_query ("Unlock tables")


return true;


Else


return false;


}





destructor


function __destruct () {


if ($this-&gt;db) {


mysql_query ("Unlock Tables", $this-&gt;db);


Mysql_close ($this-&gt;db);


}


}


}


?&gt;

I hope this article will help you with your PHP program design.

Related Article

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.