Why don't use  file_put_contents ?
>From the docs:
file_put_contents
Write a string to a file (PHP 5)

int file_put_contents ( string filename, mixed data [, int flags [,
resource context]] )

Identical to calling fopen(), fwrite(), and fclose() successively.

You can also specify the data parameter as an array (not multi-
dimension arrays). This is equivalent to file_put_contents($filename,
implode('', $array)).

As of PHP 5.1.0, you may also pass a stream resource to the data
parameter. In result, the remaining buffer of that stream will be
copied to the specified file. This is similar with using
stream_copy_to_stream().

Parameters
filename
The file name where to write the data

data
The data to write. Can be either a string, an array or a stream
resource (explained above).

flags
flags can take FILE_USE_INCLUDE_PATH, FILE_APPEND and/or LOCK_EX
(acquire an exclusive lock), however the FILE_USE_INCLUDE_PATH option
should be used with caution.

context
A context resource

Return Values
The function returns the amount of bytes that were written to the
file, or FALSE on failure.



On Feb 9, 11:34 pm, meerkat <[email protected]> wrote:
> Hello,
>
> I have the following code to select data from mysql db and save it in
> a json file:
>
> require_once "json_encode.php";
> $arr = array();
> $rs = mysql_query("SELECT order_id, order_no, order_no_iteration,
> estimated_completion_date, other_contact_tel FROM orders")or die
> ("Query failed: " . mysql_error() . " Actual query: " . $rs);
> while($obj = mysql_fetch_object($rs))
> {
>         $arr[] = $obj;}
>
> $json = json_encode($arr);
> $JSONFile = fopen("orders.json", "w");
> fwrite ($JSONFile,$json);
> fclose($JSONFile);
> echo $json;
>
> The "fwrite ($JSONFile,$json);" is ONLY writing 1 record to the file.
> The "echo $json;" is however writing all the records to the browser.
> Looks like a logic error? Can anyone have a look? I want to get onto
> the next stage of reading the data from the json file into the gears
> sqlite db.
> Thanks, meerkat

Reply via email to