0
$result=array();
$query = "SELECT * FROM $table";
$result = mysql_query($query, $conn);
while ($table = mysql_fetch_assoc($resouter, MYSQL_ASSOC)){
   $result[]=$table;

This code is used to retrieve data from one table my question is how to retrieve data from two tables in json which are related with foreign key i.e student{st_id,st_name,st_class} book{bk_id, author, ISBN,st_id}

if i want to retrieve the student records and all the books issued by that student in json format. How can get this thanks in advance

1
  • updaed my answer take a look an tell me how it pans out Commented Dec 7, 2010 at 5:48

3 Answers 3

1
$query = 'SELECT students.*, books.* 
      FROM students
      LEFT JOIN books
      ON students.st_id = books.st_id';
$mysql_result = mysql_query($query);
$result = array();
while ($row = mysql_fetch_assoc($mysql_result)) {
   $result[] = $row;
}
return json_encode($result);
Sign up to request clarification or add additional context in comments.

Comments

1

You use the json_encode function

would look something like this

$result=array();
$query = "SELECT * FROM $table";
$result = mysql_query($query, $conn);
while ($table = mysql_fetch_assoc($resouter, MYSQL_ASSOC)){
   $result[]=json_encode($table);

or you could use a join how a join works can be read in this following link

http://www.tizag.com/mysqlTutorial/mysqlleftjoin.php

might look something like this

$result=array();
$query = "SELECT * FROM table LEFT JOIN table2 ON table.st_id = table2.st_id";
$result = mysql_query($query, $conn);
while ($table = mysql_fetch_assoc($resouter, MYSQL_ASSOC)){
   $result[]=json_encode($table);

or if you have 2 arrays of data you can merge them by using array_merge and then json_encode it

5 Comments

i already use this function but it for a single table my question is how can i retrieve data from two tables
you do a left join i've posted alink that can be quite usefull take a look
well sir the join retrieve all the relevant records at once what i want to do is first retrieve the student of id 1 and the relevant books against that id, then student id 2 and relevant books of that id and so on.....
updated my answer again :P but really I can't see why you don't wanna use a left join
@Blogger: you only think you want to do that... use the join instead and then handle creating the proper structure form the data when you iterate over the results.
0

As breezer mentioned you shoudl use a join here so you only hit the DB once. Ther eis absolutely no need to do multiple queries. As far as structuring the data i would do something like this:

$sutdents = array();

$query = "SELECT * FROM table LEFT JOIN table2 ON table.st_id = table2.st_id";
$result = mysql_query($query, $conn);

$bookFields = array_fill_keys(array(
  'bk_id', 
  'author', 
  'ISBN'
), null);

$studentFields = array_fill_keys(array(
  'st_id',
  'st_name',
  'st_class'
), null);

$students = array();

while ($sutdent = mysql_fetch_assoc($resouter, MYSQL_ASSOC)){
   $stId = $student['st_id'];
   if(isset($students[$stId]['books'])){
      $students[$stId]['books'][] = array_intersect_key($student, $bookFields);
   } else {
     $students[$stId] = array_intersect_key($student, $studentFields);
     $students[$stId]['books'] = array(array_intersect_key($student, $bookFields));
   }
}

return json_encode($students);

obviously this format is different but i prefer things nested in logical structures. You could however do exactly what you saked like so:

$query = "SELECT * FROM table LEFT JOIN table2 ON table.st_id = table2.st_id";
$result = mysql_query($query, $conn);

$bookFields = array_fill_keys(array(
  'bk_id', 
  'author', 
  'ISBN'
), null);

$studentFields = array_fill_keys(array(
  'st_id',
  'st_name',
  'st_class'
), null);

$students = array();
$books = array();

while ($sutdent = mysql_fetch_assoc($resouter, MYSQL_ASSOC)){
   $stId = $student['st_id'];

   if(!isset($students[$stId]){
     $students[$stId] = array('student' => array_intersect_key(
       $student, 
       $studentFields
     ));
   }

   if(!isset($books[$stId])){
      $books[$stId] = array(); 
   }

   $books[$stId][] = array('book' => array(array_intersect_key($student, $bookFields));
}

// convert these from assoc to numeric arrays so the come as arrays in json
$books = array_values($books);
$students = array_values($students);

// final hash looks like {students: [{student: {}}, {student: {}}], books: [{book: {}}, {book: {}}]}
return json_encode(array('students' => $students, 'books' => $books));

3 Comments

what if i use 2 queries instead of one
Probably typos (i just corrected 4 of them - should be array_fill_keys not array_fill_key), there may be others, not sure. if you want to post the error messages ill take a look.
@blogger: Please explain why you want to use 2 queries - which isnt actually two queries but 1+(the number of students) queries.

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.