0

//exporting to csv file.
// this is my code.

<?php

// Database Connection

$host="localhost";
$uname="root";
$pass="";
$database = "db_name";

$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>");


// Fetch Record from Database

$output         = "";
$table          = "information"; // Enter Your Table Name
$sql            = mysql_query("select * from $table");
$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 .="\n";

// Get Records from the table

while ($row = mysql_fetch_array($sql)) {
for ($i = 0; $i < $columns_total; $i++) {
$output .='"'.$row["$i"].'",';
}
$output .="\n";
}

// Download the file

$filename =  "myFile.csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);

echo $output;
exit;

?>

This is working very well because it is exporting a single table to a csv file.
My problem is, i want it to export from multiple tables to a single csv file.
Please, how do i do it?
Thanks in advance.

Edited by kingwash: correction

4
Contributors
3
Replies
13
Views
3 Years
Discussion Span
Last Post by chaitu11
0

JOIN tables, if the second table supplements the entries from the first table or UNION tables if the tables have the same structure

0

Yes you can do it. Just you need to decide where to place seprator(here COMMA in your case) and New Line character "\n".
or ofcourse as AndrisP suggested above you can do it using JOIN in a single query.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.