473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

writing records to text file

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
Ian

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

while($nt=mysql_fetch_array($rt)){
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]"."', ".
"$nt[AnsPerQuest]".')<br>';
}
Oct 26 '05 #1
3 2186
NC
Ian Davies wrote:

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.


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) .
");\r\n";
fwrite($fp, $line);
}
fclose($fp);

Cheers,
NC

Oct 26 '05 #2
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)
to
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.

Ian

"NC" <nc@iname.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Ian Davies wrote:

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.


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) .
");\r\n";
fwrite($fp, $line);
}
fclose($fp);

Cheers,
NC

Oct 26 '05 #3
NC
Ian Davies wrote:

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


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? :)

Cheers,
NC

Oct 26 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: JC | last post by:
I am using Mysql version: 4.0.18-max-log and trying to populate a database from a text file using source option from inside the mysql environment. The population run OK but when creating the...
0
by: Marshal Antony | last post by:
Hi, I have a web service creates a # delimited text file on a server different than the web server per day for tracking databse transactions.All transactions will be writing to this same text...
4
by: Jens Mittag | last post by:
Hi! In my code, I have an array of a structure, which I want to save to a binary file. When the array is just created, everything works fine, but when I change contents of the array, saving...
12
by: Chris Springer | last post by:
I'd like to get some feedback on the issue of storing data out to disk and where to store it. I've never been in a production environment in programming so you'll have to bear with me... My...
2
by: darrel | last post by:
Our production web site went down today. We discovered that it was because the XML file we use to create the site navigation (most all of the controls on every page rely on this XML file) was only...
5
by: UJ | last post by:
I have a system that has five programs that all communicate with each other via Message Queues. Works well. One program is a watchdog that will make sure the others are up and going. Currently I...
0
by: georges the man | last post by:
The purpose: • Sorting and Searching • Numerical Analysis Design Specification You are to write a program called “StockAnalyser”. Your program will read a text file that contains historical...
1
by: =?Utf-8?B?TWljaGFlbCBCbHVtZW50aGFsLCBNQ1NFLCBNQ0FE | last post by:
I am writing a .NET 1.1 C# windows service that writes to a dedicated event log (not the application event log, but one that I created). I want to make full use of all the fields in an event log...
1
by: Billy | last post by:
Hi All, I'm attempting to use the MapNetworkDrive <snippedbelow from entire code below with very poor performance results. Basically, I have very small 73kb text files that are rewritten daily...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.