How to extract data from mysql in json using php?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • semanticnotion
    New Member
    • Sep 2010
    • 66

    How to extract data from mysql in json using php?

    hi i have 3 tables in mysql i retrieved the data in xml using php now i want to retrieved the same data in json format but i have some problem in queries.
    i retrieved data from one table in json but unable to retrieved from three tables any help would be appreciated my code for one table is.
    Code:
    $table_first = 'abc';
    $query = "SELECT * FROM $table_first";
    $resouter = mysql_query($query, $conn);
    
    $set = array();
    
    $total_records = mysql_numrows($resouter);
    if($total_records >= 1){
    
      while ($link = mysql_fetch_array($resouter, MYSQL_ASSOC)){
        $set[] = $link;
      }
    }
    
    echo json_encode($set);
    NOTE: i dont want to retrieve data at once i.e using join query
    but want to retrieve the relevant records of particular id cause my tables are relate to other with foreign key.
    e.g the id 1 in table 1 have 5 relevant records in table2 and three records in table3 what i want to do is print the id 1 record of table1 than iterate in table2 and print all the relevant records than iterate in table3 and print the relevant records and so on for id 2 of table1.
  • JKing
    Recognized Expert Top Contributor
    • Jun 2007
    • 1206

    #2
    What is preventing you from retrieving data in three tables?

    Comment

    • semanticnotion
      New Member
      • Sep 2010
      • 66

      #3
      if does't fulfill my requirements
      ok let me confirm whats wrong with this code

      Code:
      $table_first = 'recipe';
      $query = "SELECT * FROM $table_first";
      $resouter = mysql_query($query, $conn);
      
      
      $set=array();
      while ($link = mysql_fetch_array($resouter, MYSQL_ASSOC)){
      foreach ($link as $fieldname => $fieldvalue){
          $set[]= $fieldvalue;}
       $query2="SELECT ingredients.ingredient_id,ingredients.ingredient_name,ingredients.ammount FROM ingredients where rec_id = ".$link['rec_id'];
      $result2 = mysql_query($query2, $conn);
      
       while ($rs = mysql_fetch_array($result2, MYSQL_ASSOC)){
           foreach($rs as $fieldname =>$fieldvalue){
               $set[]=$fieldvalue;
           }
      
       }
      
      }
      echo json_encode($set);
      the result of this code is:
      Code:
      ["14","Spaghetti with Crab and Arugula","http:\/\/www","","2010-11-11 14:35:11","localhost\/pics\/SpaghettiWithCrabAndArugula.jpg","7","13 ounces spaghetti","10 kg","8","1 pound crabmeat","10"]
      Note: The ingredients id starts after the image tag. 7 is the ingredient id followed by two fields "ingredient s txt and amount" then 8 is another ingredient id relevant to the recipe id.
      like there is no ({) open or (}) close bracket in my result

      what i want to do is to output it in a correct json format. i.e
      Code:
      [
       {
        "rec_id": "14",
        "name":"Spaghetti with Crab and Arugula",
        "overview":"http:\/\/www",
        "category":"category",
                      "time":"2010-11-11 14:35:11",
                      "image":"localhost\/pics\/SpaghettiWithCrabAndArugula.jpg"
        "ingredients":
         {
          "ingredient":
           [                       {"ingredient_id":"7","ingredient_name":"13ounces spaghetti","amount":"10kg" },
      { "ingredient_id": "8", "ingredient_name": "1 pound crabmeat","amount":"10kg" },
      
           ]
         }]
      this also have two arrays but in proper format. Where i am going wrong in my code. any suggestion please.....

      Comment

      • JKing
        Recognized Expert Top Contributor
        • Jun 2007
        • 1206

        #4
        Have a look at the json_encode function of php json_encode()

        Comment

        • semanticnotion
          New Member
          • Sep 2010
          • 66

          #5
          i read the manual about json_encode() but could't get the clue how to extract data from two or more tables.

          Comment

          Working...