我一直在寻找为我的数据库网站启用EXPORT功能的方法。我的目标是构建一个非常简单的PHP代码来实现这一点,它所需要的只是将我的一个MySQL表的内容导出到用户的桌面,或者给用户一个可点击的链接,以便他可以下载导出的数据库。数据库必须在Excel/CSV中(因为我将使用此功能导入/导出数据库备份功能)。
我看到fputcsv()和其他旧方法,如mysql(而不是mysqli)代码,但都失败了,有些方法在服务器的目录上创建了一个CSV文件,而其他简单方法则会出现等错误
explode()期望参数2是字符串,中给定的对象
或
fputcsv()期望参数2为数组,中给定为null
我觉得这应该很简单,但导出DB功能的最佳方式或代码是什么?
编辑:
在组合和测试了一系列选项后,我能够解决它。我在这里分享,这样那些将经历我的困境的人就不会像我那样困难:(
当用户单击EXPORT按钮时,Chrome浏览器会自动下载一个CSV文件,其中包含数据库的副本,包括字段名。
<?php
include('/connectme.php');
$thequery = "SELECT * FROM members";
$result = mysqli_query($conn,$thequery);
$file = fopen("php://output","w");
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="exported.' . date("Y.m.d") . '.csv"');
header('Pragma: no-cache');
header('Expires: 0');
$emptyarr = array();
$fieldinf = mysqli_fetch_fields($result);
foreach ($fieldinf as $valu)
{
array_push($emptyarr, $valu->name);
}
fputcsv($file,$emptyarr);
while($row = mysqli_fetch_assoc($result))
{
fputcsv($file,$row);
}
fclose($file);
mysqli_free_result($result);
mysqli_close($conn);
?>
<form class="elegant-aero" action="backup.php">
<center>
<h1>SQL Server Backup And Restore
</h1>
<style>
.btn{
width:80px;
height:30px;
background:#0B49D8;
color:#FFFFFF;
}
</style>
<label>
<input id="name" type="submit" name="backup" class="btn" value="Backup" />
</label>
</form>
**php代码如下。您可以更改扩展名.sql或.csv,并更改要保存备份文件的路径。当前备份保存在代码文件所在位置附近的文件中。dabase备份文件名是可以更改的库存**
<?php
error_reporting(E_ALL ^ E_DEPRECATED);
error_reporting(error_reporting() & ~E_NOTICE);
backup_tables('localhost','root','','stock1');
/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);
//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
//cycle through
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$return.= 'DROP TABLE '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "'n'n".$row2[1].";'n'n";
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j < $num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace("'n","''n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j < ($num_fields-1)) { $return.= ','; }
}
$return.= ");'n";
}
}
$return.="'n'n'n";
}
//save file
$handle = fopen('stock'.'.csv','w+');
fwrite($handle,$return);
fclose($handle);
if($handle)
{
//$msg="Insert Successfully!!";
echo "<script>alert('Backup success');document.location='home.php'</script>";
?>
<?php
}
else
{
//$errmsg=" Insert not success!!";
echo "<script>alert('Backup not success');document.location='home.php'</script>";
}
}
?>
一个更简洁、更快的选项是使用INFILE/OUTFILE。因此,在PHP中调用以下SQL查询。。。
$db_conn = mysql_connect('localhost','root','');
mysql_select_db('stackoverflow',$db_conn);
$q = "
SELECT * FROM mytable INTO OUTFILE '/mytable.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY ''"'
LINES TERMINATED BY ''n';
";
$result = mysql_query($q,$db_conn);
这是从后面导入数据的最快方法。
在组合和测试了一系列选项后,我能够解决它。我在这里分享,这样那些将经历我的困境的人就不会像我那样困难:(
当用户单击EXPORT按钮时,Chrome浏览器会自动下载一个CSV文件,其中包含数据库的副本,包括字段名。
<?php
include('/connectme.php');
$thequery = "SELECT * FROM members";
$result = mysqli_query($conn,$thequery);
$file = fopen("php://output","w");
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="exported.' . date("Y.m.d") . '.csv"');
header('Pragma: no-cache');
header('Expires: 0');
$emptyarr = array();
$fieldinf = mysqli_fetch_fields($result);
foreach ($fieldinf as $valu)
{
array_push($emptyarr, $valu->name);
}
fputcsv($file,$emptyarr);
while($row = mysqli_fetch_assoc($result))
{
fputcsv($file,$row);
}
fclose($file);
mysqli_free_result($result);
mysqli_close($conn);
?>