$_POST to CSV in php

  • 本文关键字:in php CSV to POST php csv
  • 更新时间 :
  • 英文 :


我有一个PHP文件将PHP数据导出到CSV。

但是我有以下错误

警告:mysql_num_fields()期望参数1是 c: xampp htdocs exporttocsv3 export2csv.php in Line 28 28 <<b> 28 <<b> 28 <<b> 28 <<b> 28 <<b>/b>

警告:mysql_fetch_array()期望参数1为 在 c: xampp htdocs exporttocsv3 export2csv.php 上给出的资源布尔值 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 .="n";
// Get Records from the table
while ($row = mysql_fetch_array($sql)) {
for ($i = 0; $i < $columns_total; $i++) {
$output .='"'.$row["$i"].'",';
}
$output .="n";
}
// Download the file
$filename =  "myFile.csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
echo $output;
exit;
}   
?>

您可以看到它在mysql中,但稍后我将其转换为mysqli查询。谢谢您的未来答案

您在查询中缺乏近括号。这就是为什么它显示错误。

 $sql = mysql_query("SELECT id, name, code FROM $table WHERE (name LIKE '%$name%')")

最新更新