0

I am trying to create an export script, to export the customers from a database table. The script works, it does that but it doesn't export the column names, only the records. That prevents me from uploading the CSV file and import the records to a blank table.

<?php

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database-name`enter code here`";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
 $filename = 'customers-'.date('d-m-Y');
 @header("Content-Disposition: attachment; filename=$filename.csv");

 $select = $conn ->query("SELECT * FROM rep_customers");
 while($row=mysqli_fetch_array($select))
 {

   $data.=$row['uid'].",";
   $data.=$row['title'].",";
   $data.=$row['name'].",";
   $data.=$row['surname'].",";
   $data.=$row['email'].",";
   $data.=$row['telephone'].",";
   $data.=$row['mobile'].",";
   $data.=$row['worknr'].",";
   $data.=$row['firstline'].",";
   $data.=$row['secondline'].",";
   $data.=$row['thirdline'].",";
   $data.=$row['town'].",";
   $data.=$row['state'].",";
   $data.=$row['postcode'].",";
   $data.=$row['country'].",";
   $data.=$row['status'].",";
   $data.=$row['notes']."\n";
 }

 echo $data;
 exit();

Can you please help me get the column names as well? Thank you.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345

0 Answers0