TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-01-2010, 09:02 PM   #1 (permalink)
The Acquainted
 
Join Date: Feb 2008
Posts: 107
Thanks: 3
CΛSTΞX is on a distinguished road
Plugin/Addon Write Mysql results to a text file

Hello mates, I need help about writing mysql results to a text file. I stuck in this code, it only write "array" to a text file.

PHP Code:
$myFile "test.txt";
$fh fopen($myFile'w') or die("can't open file");

function 
data_dump$query ) {
    
$results mysql_query$query );

    
$data = array();

    while ( 
$row = @mysql_fetch_assoc$results ) )
        
$data[] = $row;

    return 
$data;
}

$stringData data_dump$query )."\n";

fwrite($fh$stringData);
fclose($fh); 
__________________
Downloadic
infolizer
Send a message via MSN to CΛSTΞX
CΛSTΞX is offline  
Reply With Quote
Old 02-01-2010, 10:11 PM   #2 (permalink)
The Acquainted
 
Join Date: Nov 2009
Location: nr Stratford-Upon-Avon
Posts: 137
Thanks: 3
maeltar is on a distinguished road
Default

what is $query ?? nothing is defined

you could use something like

Code:
$myFile = "test.txt";
$fh = fopen($myFile, 'w') or die("can't open file"); 

$query = "select * into outfile '$myFile' from  <tablename>";
$result = mysql_query($query);

fclose($fh);
__________________
Thanks... Simon

Sex, Drugs & Linux Rules
Send a message via MSN to maeltar
maeltar is offline  
Reply With Quote
Old 02-01-2010, 10:34 PM   #3 (permalink)
The Acquainted
 
Join Date: Feb 2008
Posts: 107
Thanks: 3
CΛSTΞX is on a distinguished road
Default

Thanks, you mean like this ? It doesnt worked :(

PHP Code:
$query "select * from looks order by id";

$stringData data_dump$query )."\n";

fwrite($fh$stringData);
fclose($fh); 
__________________
Downloadic
infolizer
Send a message via MSN to CΛSTΞX
CΛSTΞX is offline  
Reply With Quote
Old 02-02-2010, 05:27 AM   #4 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

There are two issues with the script are preventing it from working. The first is that you can not simply print an array as is, arrays reference to places in memory that hold your values. You have to go though each element of the array and write the value to the file. The other issue is that you were passing the array by value, since arrays reference memory opposed to directly holding a value, you need to pass that reference. The following works, since I didn't want to mess around with the SQL, I assigned the array by hand.

PHP Code:
<?php
$myFile 
"test.txt";
$fh fopen($myFile'w') or die("can't open file");

function 
data_dump$query ) {
    
$data[] = "Hello";
    
$data[] = "World";
    
$data[] = "Again";
    
$data[] = "Dummy";
    
$data[] = "content";

    return 
$data;
}

$stringData = &data_dump$query )."\n";

foreach(
$stringData as $value)
{
    
fwrite($fh$value);
}
fclose($fh);
?>
My explaination probably did not make sense and certianly did not do the topic justice. Look up "PHP pass by reference" on Google and have a read through this thread if you want some further clarification.
__________________

Village Idiot is offline  
Reply With Quote
The Following User Says Thank You to Village Idiot For This Useful Post:
CΛSTΞX (02-02-2010)
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
php/html form to echo text file contents Hustle Absolute Beginners 2 03-10-2009 07:14 AM
Read Text File in Reverse buildakicker General 9 11-26-2008 06:43 PM
Writing to XML file buildakicker General 8 02-06-2008 08:17 PM
Outputting MySQL Results Andrew Advanced PHP Programming 5 01-13-2008 04:59 PM
Database export to text file... d4v1d Absolute Beginners 5 01-09-2008 04:30 PM


All times are GMT. The time now is 02:11 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design