php accessing mysql, trouble writing to txt file

when writing to txt file, it only writes the first item from the datab

Page 1 of 1

9 Replies - 3827 Views - Last Post: 09 April 2008 - 10:23 PM Rate Topic: -----

#1 kevin186   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-April 08

php accessing mysql, trouble writing to txt file

Posted 09 April 2008 - 01:13 PM

ok, i know how to use php to access my database and print out some good xml. but, my issue is that i want that data written directly to a .txt file also. it is for a crude link exchange. when the other users do a <?PHP readfile('http://mysite.com/somefile.txt'); ?> it will show the links where they put this code. i would rather limit them only access to the txt file instead of the php which queries the database so that i wont have so many queries happening all the time. i will run this php file after i add new entries to the database as to update the txt file that they link to. of course i could just run the php file then copy and paste that to the txt file, but that seems kinda stupid. i can write to the txt file from the php file, but it only writes the first entry of the database query. so here is my code for the php file that accesses my database
<?PHP

$link = mysql_connect("localhost","username","password");
mysql_select_db("link_exchange");

$query = 'SELECT * FROM links';
$results = mysql_query($query);

echo "<a href=" . "\"". "http://proxyhotlink.com/" . "\"" . ">" . "Join Us!" . "</a>" . " ";

while($line = mysql_fetch_assoc($results)) {
	echo "<a href=" . "\"" . "http://" . $line["url"] . "/" . "\"" . ">" . $line["name"] . "</a>" . " ";


//Writing to file (i mean trying to write to file:0)
$file = fopen("dreamincode.txt", "w+");
fwrite($file, "<a href=" . "\"". "http://proxyhotlink.com/" . "\"" . ">" . "Join Us!" . "</a>" . " " . "<a href=" . "\"" . "http://" . $line["url"] . "/" . "\"" . ">" . $line["name"] . "</a>" . " ");
fclose($file);
}

mysql_close($link);

?>
the links to my test files are http://proxyhotlink....dreamincode.php and http://proxyhotlink....dreamincode.txt . as you can see in the txt file, only the first database entry is output here along with my one static entry i will keep in the front. i am sure this is very simple for one of you experts, but damn i have no clue. thanks. the one who helps me will get to put their 468 x 60 banner in my banner rotator for a month as a thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: php accessing mysql, trouble writing to txt file

#2 jaelle   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 05-April 05

Re: php accessing mysql, trouble writing to txt file

Posted 09 April 2008 - 06:42 PM

I see two things there that might be the source of all your problems. First, you are writing an actual link and name (http://proxyhotlink.com/, Join Us) to the file instead of the information from the database ($line["url"],$line["name"]). That was probably just a typo. Second, you are opening the file in w+ mode each time you go through the loop, which will truncate the file to a size of 0 and write the content from the beginning. In essence, you are writing the one line over and over to an empty file.

I would suggest opening your file once before the loop and then closing it again after the loop. If you want to append to your file instead of truncating it, use the a+ mode for opening the file instead of w+.


//replace w+ with a+ to not truncate the file
$file = fopen("dreamincode.txt", "w+");

while($line = mysql_fetch_assoc($results)) {
	echo "<a href=" . "\"" . "http://" . $line["url"] . "/" . "\"" . ">" . $line["name"] . "</a>" . " ";

//Writing to file (i mean trying to write to file:0)
fwrite($file, "<a href=" . "\"". $line["url"] . "\"" . ">" . $line["name"] . "</a>" . " " . "<a href=" . "\"" . "http://" . $line["url"] . "/" . "\"" . ">" . $line["name"] . "</a>" . " ");
}
fclose($file);


Was This Post Helpful? 0
  • +
  • -

#3 kevin186   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-April 08

Re: php accessing mysql, trouble writing to txt file

Posted 09 April 2008 - 07:03 PM

View Postjaelle, on 9 Apr, 2008 - 06:42 PM, said:

I see two things there that might be the source of all your problems. First, you are writing an actual link and name (http://proxyhotlink.com/, Join Us) to the file instead of the information from the database ($line["url"],$line["name"]). That was probably just a typo. Second, you are opening the file in w+ mode each time you go through the loop, which will truncate the file to a size of 0 and write the content from the beginning. In essence, you are writing the one line over and over to an empty file.

I would suggest opening your file once before the loop and then closing it again after the loop. If you want to append to your file instead of truncating it, use the a+ mode for opening the file instead of w+.


//replace w+ with a+ to not truncate the file
$file = fopen("dreamincode.txt", "w+");

while($line = mysql_fetch_assoc($results)) {
	echo "<a href=" . "\"" . "http://" . $line["url"] . "/" . "\"" . ">" . $line["name"] . "</a>" . " ";

//Writing to file (i mean trying to write to file:0)
fwrite($file, "<a href=" . "\"". $line["url"] . "\"" . ">" . $line["name"] . "</a>" . " " . "<a href=" . "\"" . "http://" . $line["url"] . "/" . "\"" . ">" . $line["name"] . "</a>" . " ");
}
fclose($file);


finally a response, thank you. actually that url is there on purpose because i always need it first on the list. so maybe the w+ is the problem. i just used this code from gotoandlearn.com and i am trying to adapt it to also write a txt file with the same data. it works fine, except that darn txt file. i will try it now. thanks. by the way, i have no idea what you just said. flew over my head. i am a copy and paster and try to learn what i can.
Was This Post Helpful? 0
  • +
  • -

#4 kevin186   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-April 08

Re: php accessing mysql, trouble writing to txt file

Posted 09 April 2008 - 07:16 PM

ok, so close. it writes all the links now to the text, except for it constantly repeats that one that i am trying to have first. how can i make sure that the one link is in front, since i will be changing order of other links with ORDER BY RAND() from the database query. i just need to know where i can type in a link so that it will write to the file, but not be repeated. thanks so much. do you have your website banner picked out that you are gonna send to me?
Was This Post Helpful? 0
  • +
  • -

#5 jaelle   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 05-April 05

Re: php accessing mysql, trouble writing to txt file

Posted 09 April 2008 - 07:18 PM

I'm glad I was able to help.

You may run into another problem with that code if you want the Join Us link to come first. Copying and pasting can be dangerous, so I will attempt to explain. :)

First, you get the information from you mysql database. This returns the information to you in a way which can be fetched by the while ($line = mysql_fetch_assoc($results)) line. However, there are several things you need to do before entering your loop.

Before you can write this to a file, you have to open the file. It is best to do this outside of your loop, otherwise you will continuously open the file over and over again. So, open the file for writing first.

Next, you mentioned that you would like the Join Us link first. You will want to add this link to the file before entering the loop. Otherwise, you will find that you have a Join Us link for every row in your database.

After you have opened the file and inserted the first link, you can start reading the data out of the database and writing it to the text file. When you've looped through each row in the database, you will exit the while loop. Then you can close your file since you don't need to write to it anymore

New improved version:
<?PHP
//connect to database
$link = mysql_connect("localhost","username","password");
mysql_select_db("link_exchange");

//get all links from database
$query = 'SELECT * FROM links';
$results = mysql_query($query);

//open file for writing
$file = fopen("dreamincode.txt", "a+");

//write the first link to the text file
fwrite($file,"<a href=" . "\"". "http://proxyhotlink.com/" . "\"" . ">" . "Join Us!" . "</a> ");

echo "<a href=" . "\"". "http://proxyhotlink.com/" . "\"" . ">" . "Join Us!" . "</a>" . " ";

//loop through each row in the database
while($line = mysql_fetch_assoc($results)) {
	echo "<a href=" . "\"" . "http://" . $line["url"] . "/" . "\"" . ">" . $line["name"] . "</a>" . " ";

	//write one link to the file
	fwrite($file, "<a href=" . "\"" . "http://" . $line["url"] . "/" . "\"" . ">" . $line["name"] . "</a>" . " ");
}

//close the file, we don't need it anymore
fclose($file);

//close mysql
mysql_close($link);

?>



I hope that makes a bit more sense.
Was This Post Helpful? 0
  • +
  • -

#6 kevin186   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-April 08

Re: php accessing mysql, trouble writing to txt file

Posted 09 April 2008 - 07:59 PM

ok, it seems i am back at square one. if using "a", everytime i run the php file it appends a new link entry to the text file, but it only takes the last entry from the database, instead of all the entries. the "w" would be better in this situation because when run php, it will clear old link list and put new one in at random order. but my problem i still have is that i cannot write the entire list to the txt file. it will only write the last entry that is generated by the php file. damn i am stumped. just need to write entire contents to text file, just as it prints it out to the php file. so that i can minimize database queries.

-edit i made this reply before i saw that you replied. i will try your code. thanks

This post has been edited by kevin186: 09 April 2008 - 08:01 PM

Was This Post Helpful? 0
  • +
  • -

#7 kevin186   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-April 08

Re: php accessing mysql, trouble writing to txt file

Posted 09 April 2008 - 08:15 PM

ok, i changed it to the w+ so that it will wipe the file and write it new each time i run the php file. and also i added a random inquiry to it. here it is in action http://proxyhotlink.com/snippet2.php and here is the output http://proxyhotlink.com/snippet2.txt. each time the php file is run, the txt file changes to reflect the new order. one last thing, what is a good way to insert line breaks with the \n. i dont want to break the code. thanks and do you have a website?

-edit ok, got it just fine now. thanks alot. and my offer was to have your banner in my rotator for a month for your website. do you have a site and a 468x60 banner? thanks

This post has been edited by kevin186: 09 April 2008 - 08:39 PM

Was This Post Helpful? 0
  • +
  • -

#8 jaelle   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 05-April 05

Re: php accessing mysql, trouble writing to txt file

Posted 09 April 2008 - 09:38 PM

Actually I didn't even see the part about the banner lol. I do have a website (www.jlcreations.com - it's pretty boring today, as I'm recognizing CSS naked day and got rid of all the styles), but it is a very small site. It is just my personal portfolio. What is your website? I don't have a banner now anyway. I might have to take a rain check on that. :-P
Was This Post Helpful? 0
  • +
  • -

#9 kevin186   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-April 08

Re: php accessing mysql, trouble writing to txt file

Posted 09 April 2008 - 10:11 PM

View Postjaelle, on 9 Apr, 2008 - 09:38 PM, said:

Actually I didn't even see the part about the banner lol. I do have a website (www.jlcreations.com - it's pretty boring today, as I'm recognizing CSS naked day and got rid of all the styles), but it is a very small site. It is just my personal portfolio. What is your website? I don't have a banner now anyway. I might have to take a rain check on that. :-P


hmm, you seemed smart, but i am not so sure now. lol, nah just kidding. you didnt notice the http://proxyhotlink.com in the middle of all those links? but yeah i get a decent amount of traffic. well, not too much. so far 357 uniques and 1508 total pageviews today. so, your banner will atleast be seen once. but, your site is nice. i looked at your portfolio. i saw some flash slideshows. are you good in flash too? because i am stumped on one little thing there. if only you could be my savior. so, when you are ready, click on my banner on the flash that says "your banner here." be signed into your email first as it is a mailto link that will populate the sending data. and if you just need to contact me, use the contact form on that site. thanks a bunch.

This post has been edited by kevin186: 09 April 2008 - 10:12 PM

Was This Post Helpful? 0
  • +
  • -

#10 jaelle   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 05-April 05

Re: php accessing mysql, trouble writing to txt file

Posted 09 April 2008 - 10:23 PM

Haha yea.. I guess I should have assumed the links were from your site... Ah well, I blame the lack of caffeine. I do know some flash. My contact info is in my profile or on my website if you want to get in touch with me. Or you can just post them in the Flash forum and someone is sure to answer them in case I can't. :) If I come up with a banner in the next few days I will email you. Good luck with everything.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1