如何使用php从json中的两个mysql表中获取数据

时间:2010-12-07 05:39:35

标签: php mysql json

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

此代码用于从一个表中检索数据 我的问题是如何从json中与外键相关的两个表中检索数据,即student {st_id,st_name,st_class}     书{bk_id,作者,ISBN,st_id}

如果我想以json格式检索学生记录和该学生发出的所有书籍。怎么能提前得到这个感谢

3 个答案:

答案 0 :(得分:1)

您使用json_encode功能

看起来像这样

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

或者您可以使用联接如何在以下链接中读取联接的工作原理

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

可能看起来像这样

$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);

或者如果您有2个数据阵列 您可以使用array_merge合并它们,然后使用json_encode

答案 1 :(得分: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);

答案 2 :(得分:0)

正如breezer所说,你在这里使用了一个连接,所以你只打了一次DB。绝对不需要做多个查询。至于构建数据,我会做这样的事情:

$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);

显然这种格式不同,但我更喜欢嵌套在逻辑结构中的东西。然而,你可以完全按照你所做的那样做:

$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));