Jump to content

[SOLVED] I need to export query results to a .txt file.


JSHINER

Recommended Posts

Here's some code:

 

function get($db) {
	return $db->getRow('SELECT field_1, field_2 FROM table WHERE pull = y');
}

$page = array();

$page['results'] = get($db);

foreach ($page['results'] as $c) {

echo $field1, ',http://www.site.com/', field2, '<br>';

}

 

Which will display results as:

 

00001,http://www.site.com/020202

00002,http://www.site.com/020203

00003,http://www.site.com/020204

 

I need this info displayed on a .txt file that is updated each time that text file is called up. So if a new result is added, when that text file is called up I need that new listing to appear there. Or every time a new result is added, it updates the text file. Whichever makes the most sense, I'm not sure.

 

How can I go about doing this?

You probably haven't got an existing file... see http://us3.php.net/manual/en/function.file-put-contents.php

or

Here's an append function (if it's any use):

function file_append($fn, $s)
{
$fp = fopen($fn, "a");	// use to append
$written = 0;
while ($written == 0)	// keep trying until lock is free
{
	if (flock($fp, LOCK_EX))
	{
		fwrite($fp, $s);
		flock($fp, LOCK_UN);
		$written = 1;
	}
}
fclose($fp);

return 0;
}

$contents = file_get_contents('file.txt');

	$vara = $db->$_GET['vara'];

	$varb = $db->$_GET['varb'];

	$newdata = $contents."\n$vara,http://www.site.com/$varb";

	file_put_contents('file.txt', $newdata);

 

What is wrong with the above code? It does not write anything to the file.

$contents = file_get_contents('file.txt');

 

$vara = $db->$_GET['vara'];

 

$varb = $db->$_GET['varb'];

 

$newdata = $contents."\n$vara,http://www.site.com/$varb";

 

if (is_writable("file.txt")) {

                $txtfile = fopen('file.txt',a);

                fwrite($txtfile, $newdata);

                fclose($txtfile);

                }

 

 

should work .......

If the .txt file is being called up in a web browser, why not just change the link to the file to a .php script.  Then do the following:

 

<?php
  header("Content-type: text/plain");
  $sql = "SELECT ...";
  $q = mysql_query($sql);
  if($q){
    while($row = mysql_fetch_assoc($q)){
      echo "{$row['col1']},{$row['col2']}";
    }
  }
?>

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.