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