download csv php mysql

CREATE TABLE `users` (
  `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `username` varchar(80) NOT NULL,
  `name` varchar(50) NOT NULL,
  `gender` varchar(10) NOT NULL,
  `email` varchar(70) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

=========================================================
// this config.php
<?php
$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "tutorial"; /* Database name */

$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
 die("Connection failed: " . mysqli_connect_error());
}


=================================================================
// prints contents of db to a table with an export button
<?php 
include "config.php"; // Database connection file
?>

<div class="container">
 
 <form method='post' action='download.php'>
  <input type='submit' value='Export' name='Export'>
 
  <table border='1' style='border-collapse:collapse;'>
    <tr>
     <th>ID</th>
     <th>Username</th>
     <th>Name</th>
     <th>Gender</th>
     <th>Email</th>
    </tr>
    <?php 
     $query = "SELECT * FROM users ORDER BY id asc";
     $result = mysqli_query($con,$query);
     $user_arr = array();
     while($row = mysqli_fetch_array($result)){
      $id = $row['id'];
      $uname = $row['username'];
      $name = $row['name'];
      $gender = $row['gender'];
      $email = $row['email'];
      $user_arr[] = array($id,$uname,$name,$gender,$email);
   ?>
      <tr>
       <td><?php echo $id; ?></td>
       <td><?php echo $uname; ?></td>
       <td><?php echo $name; ?></td>
       <td><?php echo $gender; ?></td>
       <td><?php echo $email; ?></td>
      </tr>
   <?php
    }
   ?>
   </table>
   <?php 
    $serialize_user_arr = serialize($user_arr);
   ?>
  <textarea name='export_data' style='display: none;'><?php echo $serialize_user_arr; ?></textarea>
 </form>
</div>
    
    
    
    =============================================================
  //Create a new download.php file -- code below//  
    
    <?php
$filename = 'users.csv';
$export_data = unserialize($_POST['export_data']);

// file creation
$file = fopen($filename,"w");

foreach ($export_data as $line){
 fputcsv($file,$line);
}

fclose($file);

// download
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=".$filename);
header("Content-Type: application/csv; "); 

readfile($filename);

// deleting file
unlink($filename);
exit();

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
php download as csv download csv on button click from php mysql php download csv from table php download csv file example php download csv not from mysql download csv file php download csv file from mysql with php download csv code in php download as csv in php php mysql download csv php export mysql to csv download php download csv file from server download csv file in php download csv using php csv download php how to import csv file in php mysql php to download csv for php php download csv file php code to download csv file and upload to mysql php code to download csv file and insert into mysql download link csv php download csv format from sql query php download data in csv format in php download csv file in php example download csv in php php download csv export mysql database to csv PHP php export db to csv php save mysql result to csv php write sql table to csv php export sql to csv csv file from sql query php php export table to csv how to export table data in csv file using phpmyadmin in php export data from mysql to csv in php how to export data from mysql database to csv using php how to upload csv to mysql php sql table to csv download csv file php mysql php code to export data to csv file php upload csv to mysql export to csv php mysql php sql dump csv php download mysql table as csv php download csv from mysql how to create csv to mysql database tables using PHP php mysql query export to csv php mysql table to csv export mysql data to csv file using php query to csv php php export mysql query to csv how to export data to csv file in php sql to csv php export data in csv in php php export database to csv how to create a csv file in PHP with sql query php create csv using mysql data php mysql to csv export csv mysql php sql to csv in php export table in mysql in csv in mysqli command export table data using php export table mysql csv php export mysql csv php php export to csv export to csv in php sql server table into csv file php save data to csv php GET DATA FROM SQL INTO CSV PHP php export mysql data to csv file php create export file csv from sql query how to export csv to mysql PHP export csv to database PHP php mysql query to csv php export mysql database to csv script export mysql to csv php script export csv file in php mysql php export mysql to csv php export to csv from mysql download csv php mysql
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source