How to make CSV file from mysql database using PHP
This example demonstrate how to make csv file form mysql table using php. in this example csv file will contain name, username and email from user table. database connection has been takem dynamically from joomla configuration.php file. you need to change it as your requirement.
<?php
require_once "configuration.php";
$config = new JConfig();
$host = $config->host;
$db = $config->db;
$dbprefix = $config->dbprefix;
$password = $config->password;
$user = $config->user;
$con = new mysqli($host,$user,$password, $db);
$filename = "users.csv";
$fp = fopen('php://output', 'w');
$header = array("name","username","email");
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
fputcsv($fp, $header);
$query = "SELECT name, username, email FROM jnr6o_users";
$result = $con->query($query);
while($row = mysqli_fetch_row($result)) {
fputcsv($fp, $row);
}
exit;
?>
<?php
require_once "configuration.php";
$config = new JConfig();
$host = $config->host;
$db = $config->db;
$dbprefix = $config->dbprefix;
$password = $config->password;
$user = $config->user;
$con = new mysqli($host,$user,$password, $db);
$filename = "users.csv";
$fp = fopen('php://output', 'w');
$header = array("name","username","email");
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
fputcsv($fp, $header);
$query = "SELECT name, username, email FROM jnr6o_users";
$result = $con->query($query);
while($row = mysqli_fetch_row($result)) {
fputcsv($fp, $row);
}
exit;
?>
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment