Jump to content

export mysql to text file via PHP


sarab99

Recommended Posts

Hi,

 

I need help on how to export the output result of my php code below to text file.

 

 


<?php
echo "<CENTER><form method='POST' enctype='multipart/form-data' action='$row[salt]'>

user_name <br>
<input type=\"text\" name=\"login\" value=\"\">
<br>password<br>
<input type=\"text\" name=\"passwd\" value=\"\">
<br>dataBase_Name <br>
<input type=\"text\" name=\"port\"    value=\"\"> <br>
<input type=\"submit\" value=\"Show\">
<div align=\"center\"><br></form>";


$user = $_POST['login'];
$pass = $_POST['passwd'];
$dbname = $_POST['port'];

$LINK = mysql_connect ("localhost",$user,$pass) or die("Couldn't connect to mysql<br>". mysql_error());
$con = mysql_select_db($dbname) or die("Couldn't select database: $dbname<br>" . mysql_error());
$q = "select * from user";
$result = mysql_query ($q) or die("Problem with the query: $q<br>" . mysql_error());

$res = mysql_query("select * from user");
$num = mysql_num_rows($res);                     
echo '<CENTER> ' . $num . ' ';
?>

<textarea method='POST'  rows=20 cols=100 wrap=off>

<?php
$i = 1;
while ($row = mysql_fetch_assoc ($result)) {
echo sprintf("%5d",$i) . '|' . $row['username'] . '|'. $row['email'] . '|'. $row['password'] . '|'. $row['salt'] ."\n";

$i++;
}

?>

 

 

what I need is a link when I press it the file start downloading the data as txt. with same column in the script code.

 

 

any help will be highly appreciateed.

 

 

Link to comment
https://forums.phpfreaks.com/topic/110240-export-mysql-to-text-file-via-php/
Share on other sites

So have link with a get variable (or post), and store the record ID of the result in it. In this page, lookup via the db the content, the fwrite to a txt:

http://uk3.php.net/fwrite

 

Then display link to it.

 

Or, instead of making a temporary file, you could just fetch the data and send the client headers so that it makes them download it. (Google "php force download")

 

Pseudo code example:


if($_POST) {
//echo out data and send headers to suggest to the client's webbrowser that it try to download the file
}
else {
//display a form so the user can say what data he/she wants.
}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.