help with PHP script

I have a small database file and need to extract certain fields from it, and put those fields in a text file. I am completely new to programming. Would it  be possible for guidance on this piece of code, and perhaps could you tell me what to do in a very simple way ?  thanks very much

Europa MacDonaldChief slayer of dragonsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dealing with database is simple using php, and if you don't know where to begin, i do recommend this step by step tutorial that will guide you how to setup database, connect to it, retrive data and insert data

after you can retrieve data and show them in your page, you can format it they way you like and then save them to file
to do so (which is very easy)

say you formatted your database output and saved it to a variable named $data
all you have to do is
and you are done !

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Here's an example:
$dbhost= 'localhost';
$dbname = 'mydatabase';
$dbuser = 'myusername';
$dbpasswd = 'mypassword';
$link = mysql_connect($dbhost, $dbuser, $dbpasswd)
    or die('Could not connect: ' . mysql_error());
mysql_select_db($dbname) or die('Could not select database');
$fileout = "./locations.txt";
       $query = "SELECT locationid, name, street1, street2, city, state, zip, lat, lng FROM locations WHERE street1 != '' ORDER BY locationid";
        $result = mysql_query($query) or die('Query failed: ' . mysql_error());
        while ($myline = mysql_fetch_assoc($result)) {
                $output.= "<location>\n";
                $output.= "\t<locationid>" . $myline['locationid'] . "</locationid>\n";
                $output.= "\t<street1>" . $myline['street1'] . "</street1>\n";
                $output.= "\t<street2>" . $myline['street2'] . "</street2>\n";
                $output.= "\t<city>" . $myline['city'] . "</city>\n";
                $output.= "\t<state>" . $myline['state'] . "</state>\n";
                $output.= "\t<zip>" . $myline['zip'] . "</zip>\n";
                $output.= "</location>\n";
if (is_writable($fileout)) {
      $filehandle = fopen($fileout,"w");
echo $output;

Open in new window

Europa MacDonaldChief slayer of dragonsAuthor Commented:
Would this code work for multiple database entries ?
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

I'm not sure I understand what you mean.  Really, it depends on how the data you're looking for is structured.  This example, for instance, calls data from a single SQL SELECT statement pulling info from a single table.   Essentially, if you can write a single SELECT statement to JOIN the data you're interested in getting, then you could modify this code to work.  If you have to pull from different, unrelated tables (or databases), then you'd have to write multiple queries (and possibly connections).
Europa MacDonaldChief slayer of dragonsAuthor Commented:
It is one data base, and what happens is, in the evening many people put data through the web page. While they all put in the same type of data, there are many of them within the same table, but using all the same fields.

Is this clearer ?

A little.  Without knowing exactly what the database schema looks like and what you want the resulting output file to look like, though, I'm afraid the above script (and the URLs dr_dedo posted) are the best I can do.
Europa MacDonaldChief slayer of dragonsAuthor Commented:
Thanks very much for now. Im totally new to this and youve been a great help to me.

CWS (haripriya)Commented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
   Split: dr_dedo {http:#20854170} & gnar {http:#20854981}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Experts Exchange Cleanup Volunteer
Forced accept.

EE Admin
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.