How to extract mysql data into json using php?

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

    #1

    How to extract mysql data into json using php?

    i have retrieved mysql data from one table in json using the following script

    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);
    how can i retrieved data from two other tables in which there is a foreign key of this table in both of those tables. OR simply how can i retrieved data from 3 mysql tables in php.
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    I don't know what json does but the query will need two joins.
    Maybe it is a question better asked on the MySQL forum.
    I doubt you will want SELECT * when joining three tables

    Comment

    • semanticnotion
      New Member
      • Sep 2010
      • 66

      #3
      the join query returns all the data at once what i want to do is that to retrieved the relevant records. i.e for id 1 of first table there are 5 relevant records in second table and 3 records relevant to this in third table.
      i want to print the id 1 of table one than all the relevant records of table 2 and table 3.
      And same for id 2 of table 1......

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        the join query returns all the data at once what i want to do is that to retrieved the relevant records
        Which is why I suggested not to use SELECT *.
        Your SELECT string should detail which fields you require.
        I can't help anymore without the table structures and the required output in tabular form

        Comment

        • semanticnotion
          New Member
          • Sep 2010
          • 66

          #5
          my table structure is

          recipe{rec_id,n ame,overview,ca tegory,time,ima ge}
          ingredients{ing redient_id,ingr edient_name,amm ount,rec_id}
          instructions{in struction_id,in struction_text, rec_id}

          look at rec_id which is foreign key in both tables.

          so queries should be
          Code:
          $query1="select * from recipe";
          than it should iterate in table2 against the relevant id from table1;
          e.g
          Code:
          $query2="select specific_field from ingredients where rec_id='rec_id from table1';

          Comment

          • code green
            Recognized Expert Top Contributor
            • Mar 2007
            • 1726

            #6
            I suggest putting the query question on MySQL forum.
            But MySQL won't do what you are expecting it to do.
            You will need an explicit JOIN, primary-foreign keys are ignored.

            Comment

            Working...