fwrite - string question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
melomonk
Forum Newbie
Posts: 10
Joined: Thu Jul 09, 2009 10:28 pm

fwrite - string question

Post by melomonk »

Hello

I'm trying to write the results of a query to a file
and I thought I had formed a string and placed it properly with fwrite

int fwrite ( resource $handle , string $string [, int $length ] )

Can someone see what i'm doing wrong.....

Code: Select all

 
//query database
$rs = mysql_query(statement);
 
// open or create file to write to
$fp = fopen("static.txt", "w");
 
//this takes the results of the query
//separates them by a comma
//appends a URL to the first field
//and breaks the row - one row per line
 
while ($row = mysql_fetch_assoc($rs)) {
    fwrite($fp, "%s, %s, %s, %s", "http://.php?id=" . $row["field1"], $row["field2"], $row["field3"], $row["field4"] . "\n");
}
 
// close file pointer
fclose($fp);
 
//kills the returned results
mysql_free_result($rs);
 
Thank you for taking the time
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: fwrite - string question

Post by McInfo »

What are you trying to write to the file?

The problem is that you are calling fwrite() with five arguments when it accepts only three and only one of those three is the string to be written to the file.

Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 2:51 pm, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: fwrite - string question

Post by requinix »

You're treating fwrite as if it were fprintf.
melomonk
Forum Newbie
Posts: 10
Joined: Thu Jul 09, 2009 10:28 pm

Re: fwrite - string question

Post by melomonk »

any suggestions on how I should write it

thanks again
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: fwrite - string question

Post by requinix »

-_-
melomonk
Forum Newbie
Posts: 10
Joined: Thu Jul 09, 2009 10:28 pm

Re: fwrite - string question

Post by melomonk »

i appreciate your time thanks
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: fwrite - string question

Post by Darhazer »

You can replace

Code: Select all

fwrite($fp, "%s, %s, %s, %s", "http://.php?id=" . $row["field1"], $row["field2"], $row["field3"], $row["field4"] . "\n");
With

Code: Select all

fwrite($fp, sprintf("%s, %s, %s, %s", "http://.php?id=" . $row["field1"], $row["field2"], $row["field3"], $row["field4"] . "\n"));
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: fwrite - string question

Post by requinix »

Or, as I tried to hint at,

Code: Select all

fprintf($fp, "%s, %s, %s, %s", "http://.php?id=" . $row["field1"], $row["field2"], $row["field3"], $row["field4"] . "\n");
Really. All you had to do was change the function.
Post Reply