[Yii Framework] export / import sql file with PHP in Yii Framework
2011-12-30 00:03
405 查看
That is just a simple example here:
<?php
/**
*
* Export & import sql
*
* @author davidhhuan
*/
class DLDatabaseHelper
{
/**
* Export the sql to a file
*
* @author davidhhuan
* @param bool $withData: Whether to export the insert-data at the same time
* @param bool $dropTable: Add to drop the table or not
* @param string $saveName: the saved file name
* @param string $savePath
*
* @return mixed
*/
public static function export($withData = true, $dropTable = false, $saveName = null, $savePath = false)
{
$pdo = Yii::app()->db->pdoInstance;
$mysql = '';
$statments = $pdo->query("show tables");
foreach ($statments as $value)
{
$tableName = $value[0];
if ($dropTable === true)
{
$mysql.="DROP TABLE IF EXISTS `$tableName`;\n";
}
$tableQuery = $pdo->query("show create table `$tableName`");
$createSql = $tableQuery->fetch();
$mysql.=$createSql['Create Table'] . ";\r\n\r\n";
if ($withData != 0)
{
$itemsQuery = $pdo->query("select * from `$tableName`");
$values = "";
$items = "";
while ($itemQuery = $itemsQuery->fetch(PDO::FETCH_ASSOC))
{
$itemNames = array_keys($itemQuery);
$itemNames = array_map("addslashes", $itemNames);
$items = join('`,`', $itemNames);
$itemValues = array_values($itemQuery);
$itemValues = array_map("addslashes", $itemValues);
$valueString = join("','", $itemValues);
$valueString = "('" . $valueString . "'),";
$values.="\n" . $valueString;
}
if ($values != "")
{
$insertSql = "INSERT INTO `$tableName` (`$items`) VALUES" . rtrim($values, ",") . ";\n\r";
$mysql.=$insertSql;
}
}
//$mysql.="/*-----------------------------------------------------*/\n\r";
}
ob_start();
echo $mysql;
$content = ob_get_contents();
ob_end_clean();
$content = gzencode($content, 9);
if (is_null($saveName))
{
$saveName = date('YmdHms') . ".sql.gz";
}
if ($savePath === false)
{
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: Download SQL Export");
header('Content-Disposition: attachment; filename='.$saveName);
echo $content;
die();
}
else
{
$filePath = $savePath ? $savePath . '/' . $saveName : $saveName;
file_put_contents($filePath, $content);
echo "Database file saved: " . $saveName;
}
}
/**
* import sql from a *.sql file
*
* @author davidhhuan
* @param string $file: with the path and the file name
*
* @return mixed
*/
public static function import($file = '')
{
$pdo = Yii::app()->db->pdoInstance;
try
{
if (file_exists($file))
{
$sqlStream = file_get_contents($file);
$sqlStream = rtrim($sqlStream);
$newStream = preg_replace_callback("/\((.*)\)/", create_function('$matches', 'return str_replace(";"," $$$ ",$matches[0]);'), $sqlStream);
$sqlArray = explode(";", $newStream);
foreach ($sqlArray as $value)
{
if (!empty($value))
{
$sql = str_replace(" $$$ ", ";", $value) . ";";
$pdo->exec($sql);
}
}
//echo "succeed to import the sql data!";
return true;
}
}
catch (PDOException $e)
{
echo $e->getMessage();
exit;
}
}
}
Have fun with Yii!
<?php
/**
*
* Export & import sql
*
* @author davidhhuan
*/
class DLDatabaseHelper
{
/**
* Export the sql to a file
*
* @author davidhhuan
* @param bool $withData: Whether to export the insert-data at the same time
* @param bool $dropTable: Add to drop the table or not
* @param string $saveName: the saved file name
* @param string $savePath
*
* @return mixed
*/
public static function export($withData = true, $dropTable = false, $saveName = null, $savePath = false)
{
$pdo = Yii::app()->db->pdoInstance;
$mysql = '';
$statments = $pdo->query("show tables");
foreach ($statments as $value)
{
$tableName = $value[0];
if ($dropTable === true)
{
$mysql.="DROP TABLE IF EXISTS `$tableName`;\n";
}
$tableQuery = $pdo->query("show create table `$tableName`");
$createSql = $tableQuery->fetch();
$mysql.=$createSql['Create Table'] . ";\r\n\r\n";
if ($withData != 0)
{
$itemsQuery = $pdo->query("select * from `$tableName`");
$values = "";
$items = "";
while ($itemQuery = $itemsQuery->fetch(PDO::FETCH_ASSOC))
{
$itemNames = array_keys($itemQuery);
$itemNames = array_map("addslashes", $itemNames);
$items = join('`,`', $itemNames);
$itemValues = array_values($itemQuery);
$itemValues = array_map("addslashes", $itemValues);
$valueString = join("','", $itemValues);
$valueString = "('" . $valueString . "'),";
$values.="\n" . $valueString;
}
if ($values != "")
{
$insertSql = "INSERT INTO `$tableName` (`$items`) VALUES" . rtrim($values, ",") . ";\n\r";
$mysql.=$insertSql;
}
}
//$mysql.="/*-----------------------------------------------------*/\n\r";
}
ob_start();
echo $mysql;
$content = ob_get_contents();
ob_end_clean();
$content = gzencode($content, 9);
if (is_null($saveName))
{
$saveName = date('YmdHms') . ".sql.gz";
}
if ($savePath === false)
{
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: Download SQL Export");
header('Content-Disposition: attachment; filename='.$saveName);
echo $content;
die();
}
else
{
$filePath = $savePath ? $savePath . '/' . $saveName : $saveName;
file_put_contents($filePath, $content);
echo "Database file saved: " . $saveName;
}
}
/**
* import sql from a *.sql file
*
* @author davidhhuan
* @param string $file: with the path and the file name
*
* @return mixed
*/
public static function import($file = '')
{
$pdo = Yii::app()->db->pdoInstance;
try
{
if (file_exists($file))
{
$sqlStream = file_get_contents($file);
$sqlStream = rtrim($sqlStream);
$newStream = preg_replace_callback("/\((.*)\)/", create_function('$matches', 'return str_replace(";"," $$$ ",$matches[0]);'), $sqlStream);
$sqlArray = explode(";", $newStream);
foreach ($sqlArray as $value)
{
if (!empty($value))
{
$sql = str_replace(" $$$ ", ";", $value) . ";";
$pdo->exec($sql);
}
}
//echo "succeed to import the sql data!";
return true;
}
}
catch (PDOException $e)
{
echo $e->getMessage();
exit;
}
}
}
Have fun with Yii!
相关文章推荐
- The import org.springframework.stereotype.Service conflicts with a type defined in the same file
- Getshell Via phpmyadmin SQL Execution In /import.php To Write Evil Webshell File Into Disk
- [Yii Framework] How to run cron job (or use command) in the server with yii framework
- java类中导入@Test单元测试注解报错:The import org.junit.Test conflicts with a type defined in the same file
- Import/Export Excel (.Xlsx) or (.Xls) File into SQL Server
- 单元测试常见错误:The import org.junit.Test conflicts with a type defined in the same file
- mysql Convert/Import MWB to SQL File in workbench
- MySQL Import and Export (.sql file) via PhpMyAdmin
- Array of country list in PHP with Zend Framework
- 【Android】Android import和export使用说明 及 export报错:jarlist.cache: Resource is out of sync with the file syst解决
- [Yii Framework] Share the session with memcache in Yii
- Android import和export使用说明 及 export报错:jarlist.cache: Resource is out of sync with the file syst解决
- The import org.junit.Test conflicts with a type defined in the same file报错
- Array of country list in PHP with Zend Framework
- Import .bak file to a database in SQL server
- Oracle: import tables use .dmp file in PL/SQL Developer
- [Yii Framework] Share the session with memcache in Yii
- SQL Server tables export/import with bcp
- The import java.util.Collection conflicts with a type defined in the same file