A- A+

PHP single file implementation MySQL backup export SQL database code

06, 14 2015 Code No comment Read 835 times

Single file can realize backup of MySQL database and generate.Sql suffix. No logon phpMyAdmin is required to export data.

  1. / / backup database
  2. $T_Host = "Localhost" ; / / database address
  3. $user = " ; / / database account number
  4. $password = " ; / / database password
  5. $dbname = " ; / / database name
  6. / / here, the account number, password and name are all transmitted from the page.
  7. If (mysql_connect! $host , $user , $password ) / / connect to MySQL database
  8. {
  9.          Echo The database connection failed. Please check and try again.
  10.      Exit ;
  11. }
  12. If (mysql_select_db! $dbname ) / / does the database exist?
  13. {
  14.          Echo There is no database: '. $dbname . please check and try again.
  15.      Exit ;
  16. }
  17. Mysql_query ( "Set names'utf8'" );
  18. $mysql = "Set charset utf8; \r\n" ;
  19. $q1 = mysql_query ( "Show tables" );
  20. While ( $t = mysql_fetch_array ( $q1 )
  21. {
  22.      $table = $t [0];
  23.      $q2 = mysql_query ( "Show create table `$table`" );
  24.      $sql = mysql_fetch_array ( $q2 );
  25.      $mysql = =. $sql ['Create Table']. "\r\n" ;
  26.      $q3 = mysql_query ( "Select * from `$table`" );
  27.      While ( $data = mysql_fetch_assoc ( $q3 )
  28. {
  29.          $keys = Array_keys ( $data );
  30.          $keys = Array_map () Addslashes ', $keys );
  31.          $keys = join (','). $keys );
  32.          $keys = ` ' . $keys . ` ' ;
  33.          $vals = Array_values ( $data );
  34.          $vals = Array_map () Addslashes ', $vals );
  35.          $vals = join ( "," , $vals );
  36.          $vals = '' . $vals . '' ;
  37.          $mysql = =. "Insert into `$table` ($keys) values ($vals); \r\n" ;
  38. }
  39. }
  40. / / storage path, by default, to the directory where the file is located.
  41. $filename = $dbname . Date ('Ymjgi'). ".sql" ; / / file naming
  42. $fp = Fopen ( $filename ('w'); / / open the file in write mode. If the file does not exist, create it.
  43. Fputs ( $fp , $mysql ); / / write content
  44. Fclose ( $fp ); / / close an open file.
  45. Echo   "Data backup is successful" ;
  46. > >

Leave a message for me.

Copyright, old madman reserves all rights.

User login

Share to: