•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 375,235 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,016 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1894 | Replies: 8 | Solved
![]() |
•
•
Join Date: May 2007
Location: Bucharest, RO
Posts: 67
Reputation:
Rep Power: 2
Solved Threads: 4
You can find it in the php manual if you do a little search 
Here is that code adjusted to what you need:
[php]
<?php
/// here is your database connection
/// than the query
$result = mysql_query ("select field_1, field_2, field_3 from table") or die (mysql_error());
$filename = $_SERVER['DOCUMENT_ROOT'].'/test.xls'; //// or whatever.txt
if (!$handle = fopen($filename, 'a')) {
print "Cannot open file ($filename)";
exit;
}
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
while ($row = mysql_fetch_row($result)) {
$row_content = implode ("\t",$row)."\r\n";
// Write $row_content to our opened file.
if (!fwrite($handle, $row_content)) {
print "Cannot write to file ($filename)";
exit;
}
}
print "Success, content wrote to file ($filename)";
fclose($handle);
} else {
print "The file $filename is not writable";
}
?>
[/php]
Try to do the basics first (like reading the manual)

Here is that code adjusted to what you need:
[php]
<?php
/// here is your database connection
/// than the query
$result = mysql_query ("select field_1, field_2, field_3 from table") or die (mysql_error());
$filename = $_SERVER['DOCUMENT_ROOT'].'/test.xls'; //// or whatever.txt
if (!$handle = fopen($filename, 'a')) {
print "Cannot open file ($filename)";
exit;
}
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
while ($row = mysql_fetch_row($result)) {
$row_content = implode ("\t",$row)."\r\n";
// Write $row_content to our opened file.
if (!fwrite($handle, $row_content)) {
print "Cannot write to file ($filename)";
exit;
}
}
print "Success, content wrote to file ($filename)";
fclose($handle);
} else {
print "The file $filename is not writable";
}
?>
[/php]
Try to do the basics first (like reading the manual)
Last edited by johny_d : May 10th, 2007 at 8:01 am.
•
•
Join Date: May 2007
Location: Bucharest, RO
Posts: 67
Reputation:
Rep Power: 2
Solved Threads: 4
Just a Tip for you, as you're new to PHP.
Once you've written to the txt file and want to get all the info from it again.
You can use the file_get_contents() function to store the file's contents into a string.
Example:
Once you've written to the txt file and want to get all the info from it again.
You can use the file_get_contents() function to store the file's contents into a string.
Example:
$str = file_get_contents("file.txt");
echo $str;
Drag.... Last edited by dr4g : May 11th, 2007 at 1:25 pm.
GardCMS :: Open Source CMS :: Gardcms.org
.xls ???
do you mean an ms excel file or something like that???
there is a classes ready to handle the .xls files if u want to get the data of an excel file.
if u r just using this extension to save text then u could do the ways above
anyway just wanted to make this thing clear
do you mean an ms excel file or something like that???
there is a classes ready to handle the .xls files if u want to get the data of an excel file.
if u r just using this extension to save text then u could do the ways above
anyway just wanted to make this thing clear
programming is an art ,only for those who can understand it.
- th3 php wr3nch -
- th3 php wr3nch -
•
•
Join Date: Sep 2007
Posts: 11
Reputation:
Rep Power: 1
Solved Threads: 1
Just what I've been looking for (for the past 4+ hours!!!) in the PHP Manual! He says "just look in the manual" -- WHERE in the manual? I've been all thru streams, wrappers, all kinds of stuff!!! The code you have here looks like JUST what I need (take mySQL query result and put into a text file) - and I SINCERELY thank you for it! BUT, where in the dang manual should I have looked! ?
http://php.net
there is a search box that will search all of the functions available, documentation, and more in php.
there is a search box that will search all of the functions available, documentation, and more in php.
toast
•
•
Join Date: Sep 2007
Posts: 11
Reputation:
Rep Power: 1
Solved Threads: 1
Yes, there sure is. And that's what I was using. Unfortunately unless you know what to search for (like - now I know I should have been looking for the term "implode" - in which case I wouldn't have needed to use the search anyway!) and you are just putting in things like "output as text file", etc., the right result doesn't particularly come up! If I there was a keywords search instead of just a rudimentary "search for words including" type thing like is the case, I would have been able to find the function without needing to know what it is called.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
- How we open a ms word document file with php? (PHP)
- Open In New Window Php (PHP)
- Repeat text from somewhere else in a document without actually writing it! (PHP)
- Problems using a php generator (PHP)
Other Threads in the PHP Forum
- Previous Thread: .htaccess
- Next Thread: problem plz help


Linear Mode