Back up the php code of the mysql database (one table and one file ). Copy the code as follows :? Php $ cmd_dbhostlocalhost; mysql host $ cmd_dbnamesq_test; database name $ cmd_dbuserroot; database username $ cmd_dbpwdjb51.net; database
The code is as follows:
$ Pai_dbhost = 'localhost'; // mysql host
$ Pai_dbname = 'SQ _ test'; // database name
$ User_dbuser = 'root'; // database username
$ Export _dbpwd = 'jb51. net'; // database user password
$ Pai_db_language = 'utf8'; // database encoding
Class dbmysql {
Public static $ dbhost = 'localhost ';
Public static $ dbname;
Public static $ dbuser = 'root ';
Public static $ dbpass;
Public static $ charset = 'utf8 ';
Public static $ DB = null;
Public $ querycount = 0;
Public function _ construct ()
{
Self: $ dbhost = $ GLOBALS ['cfg _ dbhost'];
Self: $ dbname = $ GLOBALS ['cfg _ dbname'];
Self: $ dbuser = $ GLOBALS ['cfg _ dbuser'];
Self: $ dbpass = $ GLOBALS ['cfg _ dbpwd'];
Self: $ charset = $ GLOBALS ['cfg _ db_language '];
Self: connect ();
}
Public function connect (){
Self: $ DB = mysql_connect (self ::$ dbhost, self ::$ dbuser, self ::$ dbpass );
If (! Self: $ DB ){
Self: sqlError ('cannot connect to the server! '. Self: mysqlerror); exit ("unable to connect to the server! ");;
}
If (! Mysql_select_db (self: $ dbname )){
Self: sqlError ('unable to connect to the database ('. self: $ dbname .')! '. Self: mysqlerror); exit ("unable to connect to the database! ");
}
Mysql_query ("set names '". self: $ charset. "', character_set_client = binary, SQL _mode =''; ", self: $ DB );
}
Private function mysqlerror (){
Return mysql_error ();
}
Public function getTablesName (){
$ Res = mysql_query ('Show tables from '. self: $ dbname, self: $ DB );
$ Tables = array ();
While ($ row = mysql_fetch_row ($ res) $ tables [] = $ row [0];
Mysql_free_result ($ res );
Return $ tables;
}
Public function getFields ($ table ){
$ Res = mysql_query ('describe'. $ table, self: $ DB );
$ Tables = array ();
While ($ row = mysql_fetch_row ($ res) $ tables [] = $ row [0];
Mysql_free_result ($ res );
Return $ tables;
}
Public function fetch_array ($ SQL ){
$ Res = mysql_query ($ SQL, self: $ DB );
$ R = mysql_fetch_array ($ res );
Mysql_free_result ($ res );
Return $ r;
}
Public function fetch_assoc ($ SQL ){
$ Q3 = mysql_query ($ SQL, self: $ DB); $ ra = array ();
While ($ data = mysql_fetch_assoc ($ q3 )){
$ Ra [] = $ data;
}
Mysql_free_result ($ q3 );
Return $ ra;
}
Private function sqlError ($ message = '', $ info ='', $ SQL = '') {// Save the error message to the file
Echo "{". $ message ."
DATE: ". date ('Y-n-j H: I: s ')."
ERROR: ". $ info ."
SQL: ". $ SQL ."
}
";
}
Public function close (){
Self: $ DB = null;
}
Public function _ destruct ()
{
Self: close ();
}
}
/* --- Class end */
Function makedir ($ dirpath ){
If (! $ Dirpath) return 0;
$ Dirpath = str_replace ("\", "/", $ dirpath); $ mdir = "";
Foreach (explode ("/", $ dirpath) as $ val ){
$ Mdir. = $ val ."/";
If ($ val = "..." | $ val = ".") continue;
If (! Is_dir ($ mdir )&&! File_exists ($ mdir )){
If (! @ Mkdir ($ mdir, 0755 )){
Exit ("creating Directory [". $ mdir. "] failed .");
}
}
}
Return true;
}
Function delDirAndFile ($ dirName ){
If ($ handle = opendir ($ dirName )){
While (false! ==( $ Item = readdir ($ handle ))){
If ($ item! = "." & $ Item! = ".."){
If (is_dir ("$ dirName/$ item ")){
DelDirAndFile ("$ dirName/$ item ");
} Else {unlink ("$ dirName/$ item ");}
}
}
Closedir ($ handle );
If (rmdir ($ dirName) echo "successfully deleted Directory: $ dirName
\ N ";
}
}
Function filein ($ filename = "databak/", $ table = '', $ mysql = ''){
$ Fp = fopen ($ filename. '/'. $ table. '. SQL', 'w ');
Fputs ($ fp, $ mysql );
Fclose ($ fp );
}
Header ("Content-Type: text/html; charset = utf-8 ");
$ Db = new dbmysql ();
$ Table = $ db-> getTablesName ();
$ Filename = "databak/". date ("Ymd ");
$ Url = getcwd (). "/databak /";
$ Handle = opendir ($ url );
While (false! ==( $ File = readdir ($ handle ))){
If ($ file! = "." & $ File! = ".." & Is_dir ($ url. "/". $ file )){
If (date ("Ymd")-$ file> 5) {delDirAndFile ($ url. "/". $ file );};
}
}
Makedir ($ filename );
Foreach ($ table as $ t ){
$ S1 = $ db-> fetch_array ("show create table '$ t '");
$ Mysql = "/* Time :". date ("Y-m-d H: I: s "). "*/\ r \ nDROP table if exists '$ t'; \ r \ n ". $ s1 ['create Table']. "; \ r \ n ";
$ A1 = $ db-> fetch_assoc ("select * from '$ t '");
Foreach ($ a1 as $ data ){
$ Vals = array_values ($ data );
$ Vals = array_map ('addslashes', $ vals );
$ Vals = join ("','", $ vals );
$ Vals = "'". $ vals ."'";
$ Mysql. = "insert into '$ t' VALUES ($ vals); \ r \ n ";
}
$ Mysql. = "\ r \ n ";
Filein ($ filename, $ t, $ mysql );
}
Echo "data backup successful, generate backup file". getcwd (). "/". $ filename ."/
The program automatically cleans up backups five days ago ";
?>
The http://www.bkjia.com/PHPjc/322039.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/322039.htmlTechArticle code is as follows :? Php $ cmd_dbhost = 'localhost'; // mysql host $ cmd_dbname = 'SQ _ test'; // database name $ cmd_dbuser = 'root '; // database username $ pai_dbpwd = 'jb51. net'; // database...