简体   繁体   English

如何使用PHP从JSON中的两个MySQL表中获取数据

[英]How to get data from two mysql table in json using php

$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 ie student{st_id,st_name,st_class} book{bk_id, author, ISBN,st_id} 此代码用于从一个表中检索数据我的问题是如何从json中与外键相关的两个表中检索数据,即学生{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. 如果我想检索学生记录以及该学生以json格式发行的所有书籍。 How can get this thanks in advance 怎样提前得到这个感谢

You use the json_encode function 您使用json_encode函数

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 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 或者,如果您有2个数据数组,则可以使用array_merge然后对其进行json_encode合并它们

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

As breezer mentioned you shoudl use a join here so you only hit the DB once. 正如breezer提到的那样,您应该在此处使用联接,因此您只需命中一次DB。 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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM