drp935159 2016-12-08 12:13
浏览 21
已采纳

PHP中的$ _POST到CSV

I've a php file to export php data to csv.

But I've got the following error

Warning: mysql_num_fields() expects parameter 1 to be resource boolean given in C:\xampp\htdocs\exporttocsv3\export2csv.php on line 28

and

Warning: mysql_fetch_array() expects parameter 1 to be resource boolean given in C:\xampp\htdocs\exporttocsv3\export2csv.php on line 40

Form.php

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

<form method="post" action="export2csv.php">
    <input type = "text" name ="wow" placeholder="Search by name">
<button  name="submit" type="submit">Wew</button>
</form>

</body>
</html>

export2csv.php

<?php

// Database Connection

$host="localhost";
$uname="root";
$pass="louchin";
$database = "phppot_examples";  

$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>");



if (isset($_POST['submit'])) {

$name = $_POST['wow'];

$output = "";
$table = "toy"; 
$sql = mysql_query("SELECT id, name, code FROM $table WHERE (name LIKE '%$name%'");
$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 =  "myFile.csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);

echo $output;
exit;
}   
?>

As you can see it's in mysql but I'll convert it later to mysqli query. Thank you for future answers

  • 写回答

1条回答 默认 最新

  • doucheng4094 2016-12-08 12:18
    关注

    You've got lacking close parenthesis in your query. That's why it shows an error.

     $sql = mysql_query("SELECT id, name, code FROM $table WHERE (name LIKE '%$name%')")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在