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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

$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 .......

Link to comment
Share on other sites

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']}";
    }
  }
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.