duannaxin9975
2015-09-03 14:05
浏览 41
已采纳

使用PHP文件仅从mySQL数据库导出某些列

I've created a .php file that users will access via hyperlink within our web portal to download a database to a CSV file and I can get it to download the entire database but I only want to export the columns FirstName LastName Postcode & Email.

What do I add/amend to the code below to do this? I assume it's in the mysql_query line but can't figure out the command todo this.

    <?php

// Database Connection

$host="localhost:/tmp/mysql5.sock";
$uname="dbo1234567";
$pass="password";
$database = "db1234567"; 

$connection=mysql_connect($host,$uname,$pass); 

echo mysql_error();

//or die("Database Connection Failed");
$selectdb=mysql_select_db($database) or 
die("Database could not be selected"); 
$result=mysql_select_db($database)
or die("database cannot be selected <br>");

// Fetch Record from Database

$output = "";
$table = "tbl_Coach"; // Enter Your Table Name 
$sql = mysql_query("select * from $table");
$columns_total = mysql_num_fields($sql);

// Get The Field Name

for ($i = 0; $i < $columns_total; $i++) {
$heading = mysql_field_name($sql, $i);
$output .= '"'.$heading.'",';
}
$output .="
";

// Get Records from the table

while ($row = mysql_fetch_array($sql)) {
for ($i = 0; $i < $columns_total; $i++) {
$output .='"'.$row["$i"].'",';
}
$output .="
";
}

// Download the file

$filename = "coachdb.csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);

echo $output;
exit;

?>

图片转代码服务由CSDN问答提供 功能建议

我创建了一个.php文件,用户可以通过我们的门户网站中的超链接访问该文件,以便将数据库下载到 CSV文件,我可以让它下载整个数据库,但我只想导出列FirstName LastName Postcode&amp; 电子邮件。

我如何添加/修改以下代码才能执行此操作? 我假设它在mysql_query行中,但无法找出命令todo this。

 &lt;?php 
 
 //数据库连接
 
 $ host  =“localhost:/tmp/mysql5.sock”; 
 $ uname =“dbo1234567”; 
 $ pass =“password”; 
 $ database =“db1234567”;  
 
 $的连接的mysql_connect =($主机,$ UNAME,$通);  
 
echo mysql_error(); 
 
 //或死(“数据库连接失败”); 
 $ selectdb = mysql_select_db($ database)或
die(“无法选择数据库”);  
 $ result = mysql_select_db($ database)
ot die(“无法选择数据库&lt; br&gt;”); 
 
 //从数据库中获取记录
 
 $ output =“”; 
 $ table  =“tbl_Coach”;  //输入你的表名
 $ sql = mysql_query(“select * from $ table”); 
 $ columns_total = mysql_num_fields($ sql); 
 
 //获取字段名称
 
for($ i  = 0; $ i&lt; $ columns_total; $ i ++){
 $ heading = mysql_field_name($ sql,$ i); 
 $ output。='“'。$ heading。'”,'; 
} \  n $ output。=“
”; 
 
 //从表中获取记录
 
而($ row = mysql_fetch_array($ sql)){
for($ i = 0; $ i&lt; $ columns_total  ; $ i ++){
 $ output。='“'。$ row [”$ i“]。'”,'; 
} 
 $ output。=“
”; 
} 
 
  //下载文件
 
 $ filename =“coachdb.csv”; 
header('Content-type:application / csv'); 
header('Content-Disposition:attachment; filename ='。$ filename);  
 
echo $ output; 
exit; 
 
?&gt; 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpkrbe395930 2015-09-03 14:11
    已采纳

    Just edit the

    select *
    

    bit, like

    select FirstName, LastName, Postcode, Email from $table
    

    (and try not to use the deprecated mysql_* functinons, use PDO instead if possible).

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题