PHP MySQL data import and export, data table structure Import and Export _php tutorial

Source: Internet
Author: User
Tags php mysql
Realize DataImport and export of the data table structure, import and export
********************************************************/

//
Contains MySQL DatabaseManipulating files
//
Require_once ("mysqldb.php");

/*******************************************************
* * Class Name: MySQLdb
* * Class Number: lcq-db-003
* * Role: Database linksThe establishment, data manipulation, acquisition FieldNumber, number of record bars, etc.
* * Author: Lam Chiu flag
* * Date of writing: (2007-04-07)
********************************************************/
Class Dbmanagement implements Idbmanagement
{
//
Name of all data tables in the current database
//
Private $TablesName;
//
Default Path
//
Private $DefaultPath;
//
The name of the database currently being manipulated
//
Private $DatabaseName;
//
Objects that manipulate the database
//
Private $db;

/*******************************************************
* * Method Name: __construct
** functionDescription: Create an Dbmanagement object
* * Input parameters: $_databasename-string <要操作的数据库名,如果为空则从配置文件中读取>
* * $_defaultpath-string <存储数据的默认路径,如果为空则从配置文件中读取>
* * OUTPUT parameter: None
* * Return value: None
* * Author: Lam Chiu flag
* * Date: 2007-04-09
* * Modified by Person:
* * Date:
********************************************************/
function __construct ($_databasename= "", $_defaultpath= "")//
{
Require ("config.inc.php");
if (!$_databasename) {$this->databasename= $dbName;}
else {$this->databasename=$_databasename;}

if (!$_defaultpath) {$this->defaultpath= $defaultPath;}
else {$this->defaultpath=$_defaultpath;}
$path =realpath ($this->defaultpath);
$this->defaultpath=str_replace ("", "/", $path);
$this->db=new dbfactory ();
$this->db=new mysqldb ();
}

/*******************************************************
* * Method Name: Gettablesname
* * Function Description: Get the name of all $this->database data tables
* * Input parameters: None
* * OUTPUT parameter: None
* * return value:-array < $this->tablesname: Name of all data tables for $this->database >
* * Author: Lam Chiu flag
* * Date: 2007-04-09
* * Modified by Person:
* * Date:
********************************************************/
protected function Gettablesname ()
{
$result = $this->db->query ("Show Table Status");
while ($Row = $this->db->nextrecord ($result))
{
$this->tablesname[]= $Row ["Name"];
}
return $this->tablesname;
}

/*******************************************************
* * Method Name: Getdatafilename
* * Function Description: Gets the physical filename of the data file corresponding to all data tables of $this->database
* * Input parameters: None
* * OUTPUT parameter: None
* * return value:-array < $DataFilesName: the physical filename of the data file corresponding to all $this->database data tables >
* * Author: Lam Chiu flag
* * Date: 2007-04-09
* * Modified by Person:
* * Date:
********************************************************/
protected function Getdatafilename ()
{
$this->gettablesname ();
$count =count ($this->gettablesname ());
for ($i =0; $i < $count; $i + +)
{
$DataFilesName []= $this->defaultpath.] /". $this->tablesname[$i].". TXT ";
echo $DataFilesName [$i];
}
return $DataFilesName;
}

/*******************************************************
* * Method Name: Savetablestructure
* * Function Description: Save the structure of the data table to the Install.sql file, the target path is $defaultpath
* * Input parameters: None
* * OUTPUT parameter: None
* * return value:-bool <返回为true表示保存成功;
<>
* * False indicates save failed >
* * Author: Lam Chiu flag
* * Date: 2007-04-09
* * Modified by Person:
* * Date:
********************************************************/
protected function Savetablestructure ($text)
{
$fileName = $this->defaultpath. " /install.sql ";
if (file_exists ($fileName))
//{
Unlink ($fileName);
//}
Echo $text;
$FP =fopen ($fileName, "w+");
Fwrite ($fp, $text);
}

/*******************************************************
* * Method Name: Restoretablestructure
* * Function Description: Backup the structure of all data tables in $this->database
* * Input parameters: None
* * OUTPUT parameter: None
* * return value:-bool <返回为true表示所有数据表的结构备份成功;
<>
* * = False to indicate the structure of all or part of the data table backup failed >
* * Author: Lam Chiu flag
* * Date: 2007-04-09
* * Modified by Person:
* * Date:
********************************************************/
protected function Backuptablestructure ()
{
$i = 0;
$sqlText = "";

$this->gettablesname ();
$count =count ($this->tablesname);
//
Remove the structure of all data tables
//
while ($i < $count)
{
$tableName = $this->tablesname[$i];
$result = $this->db->query ("Show create Table $tableName");
$this->db->nextrecord ($result);
//
Remove the genetic table SQL Statement
//
$sqlText. = "DROP TABLE IF EXISTS '". $tableName. " `; ";//`
$sqlText. = $this->db->getfield (1). ";";
$i + +;
}
$sqlText =str_replace ("R", "", $sqlText);
$sqlText =str_replace ("n", "", $sqlText);
$sqlText =str_replace ("'", "'", $sqlText);
$this->savetablestructure ($sqlText);
}

/*******************************************************
* * Method Name: Restoretablestructure
* * Function Description: Restore the structure of all data tables in $this->database
* * Input parameters: None
* * OUTPUT parameter: None
* * return value:-bool <返回为true表示所有数据表的结构还原成功;
<>
* = False to indicate a structural restore failure for all or part of the data table >
* * Author: Lam Chiu flag
* * Date: 2007-04-09
* * Modified by Person:
* * Date:
********************************************************/
protected function Restoretablestructure ()
{
$fileName = $this->defaultpath. " /install.sql ";
if (!file_exists ($fileName)) {echo "Cannot find the table structure file, make sure you have done a backup before!"; Exit;}
$FP =fopen ($fileName, "R");

$sqlText =fread ($fp, FileSize ($fileName));
$sqlText =str_replace ("R", "", $sqlText);
$sqlText =str_replace ("n", "", $sqlText);
$sqlArray =explode (";", $sqlText);
Try
{
$count =count ($sqlArray);
//
The last of the array is ";", which is a useless statement,
//
for ($i =1; $i < $count; $i + +)
{
$sql = $sqlArray [$i-1]. ";";
$result = $this->db->executesql ($sql);
if (! MySQL_errno ())
{
if ($i%2==0) {echo data sheet. ( $i/2). " The structure recovers successfully!n ";}
}
Else
{
if ($i%2==0)
{
echo "Data Sheet". ($i/2). " The structural recovery failed!n ";
Exit ();
}
}
}
}
catch (Exception $e)
{
$this->db->showerror ($e->getmessage ());
$this->db->close ();
return false;
}
}

/*******************************************************
* * Method Name: ImportData
* * Function Description: Import data from all data tables in $this->database from a. txt file with the same name, the source path is $defaultpath
* * Input parameters: None
* * OUTPUT parameter: None
* * return value:-bool <返回为true表示所有数据表的数据导入成功;
<>
* = False indicates data import failure for all or part of the data table >
* * Author: Lam Chiu flag
* * Date: 2007-04-09
* * Modified by Person:
* * Date:
********************************************************/
function ImportData ()
{
$DataFilesName = $this->getdatafilename ();
$count =count ($this->tablesname);
$this->db->executesql ("set character set GBK");
for ($i =0; $i < $count; $i + +)
{
$DataFilesName [$i]=str_replace ("", "/", $DataFilesName [$i])
echo $DataFilesName [$i];
$sqlLoadData = "Load Data infile". $DataFilesName [$i]. "' into table '. $this->tablesname[$i];
$sqlCleanData = "Delete from". $this->tablesname[$i];
echo $sql. " n ";
Try
{
$this->db->executesql ("Set character set UTF8");
$this->db->executesql ("SET NAMES ' UTF8 '");
$this->db->executesql ($sqlCleanData);
$this->db->executesql ($sqlLoadData);
return true;
echo "Data import successful!";
}
catch (Exception $e)
{
$this->db->showerror ($e->getmessage ());
return false;
Exit ();
}
}
}

/*******************************************************
* * Method Name: ExportData
* * Function Description: Export data from all data tables in $this->database to a. txt file with the same name, the destination path is $defaultpath
* * Input parameters: None
* * OUTPUT parameter: None
* * return value:-bool <返回为true表示所有数据表的数据导出成功;
<>
* * False indicates data export failure for all or part of the data table >
* * Author: Lam Chiu flag
* * Date: 2007-04-09
* * Modified by Person:
* * Date:
********************************************************/
function ExportData ()
{
$DataFilesName = $this->getdatafilename ();
$count =count ($this->tablesname);
Try
{
for ($i =0; $i < $count; $i + +)
{
$sql = "SELECT * from". $this->tablesname[$i]. "Into outfile". $DataFilesName [$i]. "'";
if (file_exists ($DataFilesName [$i]))
{
Unlink ($DataFilesName [$i]);
}
$this->db->executesql ("SET NAMES ' UTF8 '");
$this->db->executesql ($sql);
}
return true;
echo "Data export succeeded!";
}
catch (Exception $e)
{
$this->db->showerror ($e->getmessage ());
return false;
Exit ();
}
}

/*******************************************************
* * Method Name: BackupDatabase
* * Function Description: Backup the structure and data of all data tables in $this->database
* * Input parameters: None
* * OUTPUT parameter: None
* * return value:-bool <返回为true表示所有数据表的数据库备份成功;
<>
* * False indicates database backup failure for all or part of the data table >
* * Author: Lam Chiu flag
* * Date: 2007-04-10
* * Modified by Person:
* * Date:
********************************************************/
function BackupDatabase ()
{
Try
{
$this->backuptablestructure ();
$this->exportdata ();
return true;
}
catch (Exception $e)
{
$this->db->showerror ($e->getmessage ());
return false;
Exit ();
}
}

/*******************************************************
* * Method Name: Restoredatabase
* * Function Description: Restore the structure and data of all data tables in $this->database
* * Input parameters: None
* * OUTPUT parameter: None
* * return value:-bool <返回为true表示所有数据表的数据库还原成功;
<>
* * False indicates database restore failure for all or part of the data table >
* * Author: Lam Chiu flag
* * Date: 2007-04-10
* * Modified by Person:
* * Date:
********************************************************/
function Restoredatabase ()
{
Try
{
$this->restoretablestructure ();
$this->importdata ();
return true;
}
catch (Exception $e)
{
$this->db->showerror ($e->getmessage ());
return false;
Exit ();
}
}
}
? >[/php]


http://www.bkjia.com/PHPjc/444994.html www.bkjia.com true http://www.bkjia.com/PHPjc/444994.html techarticle Import and export of data, data table structure Import and export ********************************************************/////include MySQL database operation file//Require_ Once (Mysql ...

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