how to download table data from mysql table using php other than files

The simplest way to download table data is to use phpMyAdmin.


Matti Ressler
Suomedia

You need to give a little more information - what do you want to do with the data? Display it in a table on a web page or just make a text backup?

I want to download the data as a text fille.

Then I agree with Suomedia. I use SQLYog, though. It's a really nice query analyser type tool that you install on your local PC. It will allow you to do almost anything with your database.

www.webyog.com - there is an enterprise edition and a freebie community edition.

I would like to know whether all the data in the database/table can be available in a text file without the sql queries in it...

Yes. If you use phpmyadmin you will see it as export options in many formats. One that I commonly use is CSV.


Matti Ressler
Suomedia

is it possible using PHP code. because I want the user to download the data from the database as a text file. The concept is, user can enter some data in the database and whenever they wants, they could download it as a text file.

Yes, of course it can be done with php. Its quite a bit of code which I don't have the time for, but I am sure somebody else probably will. The web is full of tutorials on how to do this.


Matti Ressler
Suomedia

For future reference for others, this will download as *.csv file. You may change file type and HEADER types, for *.txt

I do not use (or download) any *.txt, so I have not reseached modifying.

<?php
$host = 'localhost';
$user = 'dbUsername';
$pass = 'dbPassword';
$db = 'dbName';
$table = 'dbTable';
$file = 'export';

$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");

$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field']."; ";
$i++;
}
}
$csv_output .= "\n";

$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j]."; ";
}
$csv_output .= "\n";
}

$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.