0

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

$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.

3
Contributors
3
Replies
13
Views
6 Years
Discussion Span
Last Post by anson davis
0

that title's a bit deceptive huh?

here is your standard join query:
(by the way, try to get away from doing "select *", just gets slopy after a while)

select * from table1 
inner join table2 on 
    table1.fieldname = table2.fieldname
inner join table3 on
    table2.fieldname = table3.fieldname

that is just one example where there are thousands of possibilities.

0

this will retrieve all the data at once
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 first table 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.

tip: for table 2 the query should be

$query="select table2.somefields from table2 where table2.foreign_key=table1.primaray_key";

Edited by zeeshan_kust: n/a

-1
<?php 

 include 'config.php';


 $query = "SELECT a.fee_id,a.fee_name,a.fee_amount,b.paid,b.balance FROM feelist a INNER JOIN generalfee b WHERE a.fee_class='FS 2'";
$result = mysql_query($query);

while($row=mysql_fetch_assoc($result))
    {
$json[] = $row;
    };

echo json_encode($json);


 ?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.