writing records to text file

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have the following code which displays some some formatted text in my
browser. The problem is I need to create a text file and write the records
from this array into it. Does anyone know how to acheive this.
Thanks in advance

$query="select * from Questions";
echo mysql_error();

echo "INSERT INTO tQuestions VALUES ("."$nt[QuestionNo]".",
"."$nt[TypeID]".", "."$nt[NCIndex]".", '"."$nt[Question]"."',
'"."$nt[Optiona]"."', '"."$nt[Optionb]"."', '"."$nt[Optionc]"."',
'"."$nt[Optiond]"."', '"."$nt[Optiond]"."', '"."$nt[Optione]"."',
'"."$nt[ImagePath]"."', '"."$nt[answer]"."', '"."$nt[answer2]"."',
'"."$nt[answer3]"."', '". "$nt[answer3]"."', '". "$nt[answer4]"."', '".
"$nt[answer5]"."', '". "$nt[answer6]"."', '". "$nt[answer7]"."', '".
"$nt[answer8]"."', '". "$nt[answer9]"."', '". "$nt[answer10]"."', ".

Re: writing records to text file

Ian Davies wrote:
Quoted text here. Click to load it

Shouldn't be too difficult...

$query = "select * from Questions";
$rt = mysql_query($query)
  or die(mysql_error());
$fp = fopen('mydata.txt', 'w')
  or die('Could not open the text file for writing');
while ($nt = mysql_fetch_array($rt, MYSQL_NUM)){
  $line = "INSERT INTO tQuestions VALUES (" .
          implode(',', $nt) .
  fwrite($fp, $line);  


Re: writing records to text file

Thanks Nick
Those function were exactly what Ive been searching for but to no avail.
One thing about implode can you limit how many of the fields it returns
I ask because I have modified this
implode(',', $nt)
implode("', '", $nt)
because I want quotes around some of the text (as the file will be used to
insert records, mostly text into an mysql table)
However some of the fields are numbers so they do not need quotes so your
original script
implode(',', $nt)
would work

Is there an easy way to do this without lots of code? Doesnt matter if there
isnt as I understand its ok (even desirable) to put numbers in quotes too.


Quoted text here. Click to load it

Re: writing records to text file

Ian Davies wrote:
Quoted text here. Click to load it

True, but MySQL won't mind single quotes around numbers...
The problem is, I can't remember if this was always the case
or started effective version 3.23, or started effective
version 4.0...  Anyone has memory longer than mine? :)  


Site Timeline