TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Write Mysql results to a text file (http://www.talkphp.com/general/5249-write-mysql-results-text-file.html)

CΛSTΞX 02-01-2010 09:02 PM

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


maeltar 02-01-2010 10:11 PM

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


CΛSTΞX 02-01-2010 10:34 PM

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


Village Idiot 02-02-2010 05:27 AM

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.


All times are GMT. The time now is 03:54 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0