[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

help with PHP script

Posted on 2008-02-08
9
Medium Priority
?
196 Views
Last Modified: 2013-12-12
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

michael
0
Comment
Question by:Europa MacDonald
9 Comments
 
LVL 16

Accepted Solution

by:
dr_dedo earned 1000 total points
ID: 20854170
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
http://www.freewebmasterhelp.com/tutorials/phpmysql

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
file_put_contents('fileName.txt',$data);
and you are done !
0
 
LVL 2

Assisted Solution

by:gnar
gnar earned 1000 total points
ID: 20854981
Here's an example:
<?php
 
$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";
        }
$output.="</locations>\n";
 
if (is_writable($fileout)) {
      $filehandle = fopen($fileout,"w");
      fwrite($filehandle,$output);
      fclose($filehandle);
}
 
echo $output;
?>

Open in new window

0
 

Author Comment

by:Europa MacDonald
ID: 20857589
Would this code work for multiple database entries ?
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
LVL 2

Expert Comment

by:gnar
ID: 20861679
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).
0
 

Author Comment

by:Europa MacDonald
ID: 20862998
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 ?

thanks
m
0
 
LVL 2

Expert Comment

by:gnar
ID: 20863049
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.
0
 

Author Comment

by:Europa MacDonald
ID: 20863057
Thanks very much for now. Im totally new to this and youve been a great help to me.

michael
0
 
LVL 16

Expert Comment

by:CWS (haripriya)
ID: 21168721
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.

cyberwebservice
Experts Exchange Cleanup Volunteer
0
 
LVL 1

Expert Comment

by:Computer101
ID: 21197603
Forced accept.

Computer101
EE Admin
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
The viewer will learn how to dynamically set the form action using jQuery.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question