Page 1 of 1

fwrite - string question

Posted: Sat Jul 11, 2009 1:09 am
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

Re: fwrite - string question

Posted: Sat Jul 11, 2009 1:24 am
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.

Re: fwrite - string question

Posted: Sat Jul 11, 2009 2:13 am
by requinix
You're treating fwrite as if it were fprintf.

Re: fwrite - string question

Posted: Sat Jul 11, 2009 2:32 am
by melomonk
any suggestions on how I should write it

thanks again

Re: fwrite - string question

Posted: Sat Jul 11, 2009 2:58 am
by requinix
-_-

Re: fwrite - string question

Posted: Sat Jul 11, 2009 3:28 am
by melomonk
i appreciate your time thanks

Re: fwrite - string question

Posted: Sat Jul 11, 2009 2:40 pm
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"));

Re: fwrite - string question

Posted: Sat Jul 11, 2009 4:57 pm
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.