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

Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
rolo
  • 11
  • 2

4 Answers4

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.

Rafee
  • 3,975
  • 8
  • 58
  • 88
  • 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"} – rolo Jun 28 '13 at 08:45
  • why dont you read the file and make a variable then make an `Object`. If you are using any client or server script. – Rafee Jun 28 '13 at 09:10
0

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

        }
    $response = $posts;

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

?>`

rolo
  • 11
  • 2
-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
Peter
  • 2,276
  • 4
  • 32
  • 40
-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"},}}

Rob
  • 26,989
  • 16
  • 82
  • 98
rolo
  • 11
  • 2