Import data into csv – php

<?php
mysql_connect(“localhost”,”root”,”password”);
mysql_select_db(“phpcodez”); // Dont forget to put correct database name
$filename = “users-csv”.date(‘-Y-M-d-D-H-i-s’).”.csv”;
$fp = fopen($filename, ‘w’) or die (‘file cant be opened’);

$fieldsQry = mysql_query(“SHOW COLUMNS FROM user”);
while ($fields = mysql_fetch_assoc($fieldsQry)) {
$fieldNames[] = $fields[‘Field’];
}
fputcsv($fp, $fieldNames);

$usersQry = mysql_query(“SELECT * FROM user”);
while($userInfo=mysql_fetch_assoc($usersQry)){
fputcsv($fp, $userInfo);$i++;
}

system(“chmod 777 $filename”);
fclose($fp);
echo “Imported “.$i.” items”
?>

Leave a Reply

Your email address will not be published. Required fields are marked *