1
require_once("conect.php");
$sqlString="SELECT * FROM articles;";
$response = array();
$posts = array();
$query=mysql_query($sqlString) or die (mysql_error());
    while ($row=mysql_fetch_array($query)){
        $title =$row["title"];
        $author =$row["author"];
        $article =$row["article"];
        $posts[] = array('title'=> $title, 'author'=> $author, 'article'=> $article);

    }
$response['posts'] = $posts;

$fp = fopen('json\results.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);`

result...

{"posts":[{"title":"tatoo","author":"lakmal","article":"A tattoo is a ."},  
{"title":"dog","author":"lakmal","article":"The domestic dog"},  
{"title":"cat","author":"chamikara","article":"The domestic"},  
{"title":"Automobile","author":"lakmal","article":"An automobile"}]}

i want it to save as jsonobject

4

4 Answers 4

0
$json_data = json_encode($response);
$filename ="yourfilename.json";
header('Content-type: application/json');
header('Content-Disposition: attachment; filename='.$filename);
echo $json_data ;

Is this what you are looking for.

Sign up to request clarification or add additional context in comments.

2 Comments

i got this from that code from top ---------------------------{"posts":[{"title":"tatoo","author":"lakmal","article":"A too is a ."}, {"title":"dog","author":"laal","article":"The dotic dog"}, {"title":"cat","author":"chara","article":"The dstic"}, {"title":"Automobile","author":"laal","article":"An aumobile"}]} and i want to convert to like this {"title":"tatoo","author":"laal","article":"A tattoo is a ."}, {"title":"dog","author":"laal","article":"The doic dog"}, {"title":"cat","author":"chaara","article":"The doestic"}, {"title":"Automobile","author":"laal","article":"An aule"}
why dont you read the file and make a variable then make an Object. If you are using any client or server script.
0

` $title, 'author'=> $author, 'article'=> $article);

        }
    $response = $posts;

    $fp = fopen('results.json', 'w');
    fwrite($fp, json_encode($response));
    fclose($fp);    

?>`

Comments

-1

Fetch the mysql result as an object. Dont bother converting it.

http://php.net/manual/en/function.mysql-fetch-object.php

mysql_fetch_object

Comments

-2

i found the answer

<?php
        require_once("conect.php");
        $jsonData="[{";
        $sqlString="SELECT * FROM articles;";
        $query=mysql_query($sqlString) or die (mysql_error());
            while ($row=mysql_fetch_array($query)){
                $title =$row["title"];
                $author =$row["author"];
                $article =$row["article"];
                $jsonData.='"title":"'.$title.'","author":"'.$author.'","article"'.$article.'"},';      
                $jsonData.="{";         
            }
        $jsonData = chop ($jsonData, ",");
        $jsonData.="]";


        $fp = fopen('results.json', 'w');
        fwrite($fp,json_encode($jsonData));
        fclose($fp) 

?>`

Produces:

{"title":"tatoo","author":"lakmal","article"A tattoo is a form of body modification, made by inserting indelible ink into the dermis layer of the skin to change the pigment."},}"title":"dog","author":"lakmal","article"The domestic dog (Canis lupus familiaris)[2][3] is a subspecies of the gray wolf (Canis lupus), a member of the Canidae family of the mammalian order Carnivora. The term "domestic dog" is generally used for both domesticated and feral varieties. The "},}"title":"cat","author":"chamikara","article"The domestic cat[1][2] (Felis catus[2] or Felis silvestris catus[4]) is a small, usually furry, domesticated, and carnivorous mammal. It is often called the housecat when kept as an indoor pet,[6] or simply the cat when there is no need to distinguis"},}"title":"Automobile","author":"lakmal","article"An automobile, autocar, motor car or car is a wheeled motor vehicle used for transporting passengers, which also carries its own engine or motor. Most definitions of the term specify that automobiles are designed to run primarily on roads, to have se"},}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.