将MySQL数据库以CSV格式导出到PHP中的桌面

我一直在寻找方法来启用我的数据库网站的导出function。 我的目标是构build一个非常简单的PHP代码来做到这一点,它只需要将我的一个MySQL表的内容导出到用户的桌面,或给用户一个可点击的链接,以便他可以下载导出的数据库。 该数据库必须在Excel / CSV(因为我将使用此function导入/导出数据库备份function以及)。

我看到fputcsv()和其他旧的方法,如mysql(而不是mysqli)代码,但都失败了,有些创build了一个CSV文件在服务器的目录,而其他方法简单地给出了一个错误

explode()期望参数2是string,给定的对象

要么

fputcsv()期望参数2是数组,null是给定的

我觉得这应该是简单的,但是用于导出数据库function的最佳方法或代码是什么?

编辑:

在结合和testing一系列选项后,我能够解决这个问题。 我在这里分享,所以那些会遇到我的困境的人不会像我一样难过:(

当用户点击EXPORTbutton时,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("Ymd") . '.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,并更改您要保存备份文件的path。 目前备份保存在你的代码文件所在位置附近的文件中。 数据库备份文件名是股票,你可以改变**

 <?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); 

这是后来用数据导入的最快方法。

在结合和testing一系列选项后,我能够解决这个问题。 我在这里分享,所以那些会遇到我的困境的人不会像我一样难过:(

当用户点击EXPORTbutton时,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("Ymd") . '.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); ?>