JSON数组PHP中的三个mysql表
我试图从三个相关的MySQL表构建json输出。我有一张桌子 术语表中的每一项都有几个相对的关系 “term_关系”表中由“term_分类法\u id”引用的项。每个对象_id都是一个 posts表中的主键为ID 术语JSON数组PHP中的三个mysql表,php,mysql,arrays,json,Php,Mysql,Arrays,Json,我试图从三个相关的MySQL表构建json输出。我有一张桌子 术语表中的每一项都有几个相对的关系 “term_关系”表中由“term_分类法\u id”引用的项。每个对象_id都是一个 posts表中的主键为ID 术语 +---------+------------+-----------+-------------+ | term_id | name | slug | term_group | +---------+------------+-----------+--
+---------+------------+-----------+-------------+
| term_id | name | slug | term_group |
+---------+------------+-----------+-------------+
| 12 | jewellery | jewellery | 0 |
| 13 | water | water | 0 |
+---------+------------+-----------+-------------+
+-----------+------------------+-----------+
| object_id | term_taxonomy_id | term_order|
+-----------+------------------+-----------+
| 59 | 12 | 0 |
| 57 | 12 | 0 |
| 61 | 12 | 0 |
| 62 | 13 | 0 |
| 63 | 13 | 0 |
+-----------+------------------+-----------+
+---------+--------------+-----------------------+------------------------+
| ID | post_author | post_date | post_title |
+---------+--------------+-----------------------+------------------------+
| 59 | 8 | 2015.11.30 9.33.05 | Strerding silver |
| 57 | 8 | 2015.11.30 9.34.45 | London Blue |
| 61 | 8 | 2015.11.30 9.39.34 | Strerding silver Ame |
| 62 | 9 | 2015.11.30 9.50.15 | Clean water |
| 63 | 9 | 2015.11.30 9.55.55 | 5 Liter water |
+---------+--------------+-----------------------+------------------------+
长期关系
+---------+------------+-----------+-------------+
| term_id | name | slug | term_group |
+---------+------------+-----------+-------------+
| 12 | jewellery | jewellery | 0 |
| 13 | water | water | 0 |
+---------+------------+-----------+-------------+
+-----------+------------------+-----------+
| object_id | term_taxonomy_id | term_order|
+-----------+------------------+-----------+
| 59 | 12 | 0 |
| 57 | 12 | 0 |
| 61 | 12 | 0 |
| 62 | 13 | 0 |
| 63 | 13 | 0 |
+-----------+------------------+-----------+
+---------+--------------+-----------------------+------------------------+
| ID | post_author | post_date | post_title |
+---------+--------------+-----------------------+------------------------+
| 59 | 8 | 2015.11.30 9.33.05 | Strerding silver |
| 57 | 8 | 2015.11.30 9.34.45 | London Blue |
| 61 | 8 | 2015.11.30 9.39.34 | Strerding silver Ame |
| 62 | 9 | 2015.11.30 9.50.15 | Clean water |
| 63 | 9 | 2015.11.30 9.55.55 | 5 Liter water |
+---------+--------------+-----------------------+------------------------+
帖子
+---------+------------+-----------+-------------+
| term_id | name | slug | term_group |
+---------+------------+-----------+-------------+
| 12 | jewellery | jewellery | 0 |
| 13 | water | water | 0 |
+---------+------------+-----------+-------------+
+-----------+------------------+-----------+
| object_id | term_taxonomy_id | term_order|
+-----------+------------------+-----------+
| 59 | 12 | 0 |
| 57 | 12 | 0 |
| 61 | 12 | 0 |
| 62 | 13 | 0 |
| 63 | 13 | 0 |
+-----------+------------------+-----------+
+---------+--------------+-----------------------+------------------------+
| ID | post_author | post_date | post_title |
+---------+--------------+-----------------------+------------------------+
| 59 | 8 | 2015.11.30 9.33.05 | Strerding silver |
| 57 | 8 | 2015.11.30 9.34.45 | London Blue |
| 61 | 8 | 2015.11.30 9.39.34 | Strerding silver Ame |
| 62 | 9 | 2015.11.30 9.50.15 | Clean water |
| 63 | 9 | 2015.11.30 9.55.55 | 5 Liter water |
+---------+--------------+-----------------------+------------------------+
我想从这些表创建一个JSON输出,如下所示:
{
"category": [{
"term_id": "12",
"name": "jewellery",
"slug": "jewellery",
"products": [{
"ID": "59",
"post_title": "Strerding silver",
"post_date": "2015.11.30 9.33.05",
"post_author": "8"
}, {
"ID": "57",
"post_title": "London Blue",
"post_date": "2015.11.30 9.34.45",
"post_author": "8"
}]
}, {
"term_id": "13",
"name": "water",
"slug": "water",
"products": [{
"ID": "62",
"post_title": "Clean water",
"post_date": "2015.11.30 9.50.15",
"post_author": "9"
}, {
"ID": "63",
"post_title": "5 Liter water",
"post_date": "2015.11.30 9.55.55",
"post_author": "9"
}]
}]
}
我正在使用PHP和mysql_查询方法来找出逻辑,下面是我迄今为止尝试过的代码
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$response = array();
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("look4com_lk", $dbhandle)
or die("Could not select look4com_lk");
//execute the SQL query and return records
$result = mysql_query("select *
from l4wlk_terms
INNER JOIN l4wlk_term_relationships
ON l4wlk_term_relationships.term_taxonomy_id = l4wlk_terms.term_id
INNER JOIN l4wlk_posts
ON l4wlk_term_relationships.object_id = l4wlk_posts.ID
ORDER BY l4wlk_terms.name");
//$response["infos"] = array();
$info["categorylist"] = array();
while ($row = mysql_fetch_assoc($result)) {
$arr = array();
$arr["name"] = $row["name"];
$arr["term_id"] = $row["term_id"];
$arr["post_date"] = $row["post_date"];
$arr["post_title"] = $row["post_title"];
$info[] = $arr;
}
echo json_encode($info);
//close the connection
mysql_close($dbhandle);
?>
检查下面的代码块。从
/$response[“infos”]=array()中删除代码>到echo json\u encode($info)
并粘贴下面的代码
$values = array();
while ($row = mysql_fetch_assoc($result)) {
// if term_id info not saved in the array, save it.
if (!isset($values[$row["term_id"]])) {
$values[$row["term_id"]] = array(
'term_id' => $row["term_id"],
'name' => $row["name"],
'slug' => $row["slug"]);
}
// save products under term_id.
$values[$row["term_id"]]['products'][] =
array('ID' => $row["ID"], 'post_title' => $row["post_title"],
'post_date' => $row["post_date"], 'post_author' => $row["post_author"]);
}
// removing term_ids and adding all values to a array called 'category'
$return_array['category'] = array_values($values);
echo json_encode($return_array);
在纯SQL中,这将非常困难。使用PHP可以做到这一点。您需要根据需要从mysql结果集构建阵列
/* Building PreResult Set, form here we build the required result set */
$info["categorylist"] = array();
while ($row = mysql_fetch_assoc($result)) {
$info["categorylist"][$row["term_id"]]["name"] = $row["name"];
$info["categorylist"][$row["term_id"]]["term_id"] = $row["term_id"];
$info["categorylist"][$row["term_id"]]["slug"] = $row["slug"];
$post = array();
$post["id"] = $row["id"];
$post["post_date"] = $row["post_date"];
$post["post_title"] = $row["post_title"];
$post["post_author"] = $row["post_author"];
$info["categorylist"][$row["term_id"]]["products"][] = $post;
}
/* Building Actual Result Set from PreResult Set, This step required to remove all associative key is used before in PreSet to remove redundant data*/
$json_data = array();
foreach($info["categorylist"] as $key => $value){
$products_array = array();
foreach($value["products"] as $product_keys => $products){
$products_array[] = $products;
}
$category = array();
$category["name"] = $value["name"];
$category["term_id"] = $row["term_id"];
$category["slug"] = $row["slug"];
$category["products"] = $products;
$json_data["categorylist"][] = $category;
}
echo '<pre>'.print_r($json_data,1).'</pre>';
echo '<pre>'.json_encode($json_data).'</pre>';
/*构建预结果集,从这里我们构建所需的结果集*/
$info[“categorylist”]=array();
while($row=mysql\u fetch\u assoc($result)){
$info[“categorylist”][$row[“term_id”][“name”]=$row[“name”];
$info[“categorylist”][$row[“term_id”]][“term_id”]=$row[“term_id”];
$info[“categorylist”][$row[“term_id”]][“slug”]=$row[“slug”];
$post=array();
$post[“id”]=$row[“id”];
$post[“post_date”]=$row[“post_date”];
$post[“post_title”]=$row[“post_title”];
$post[“post_author”]=$row[“post_author”];
$info[“categorylist”][$row[“term_id”]][“products”][]=$post;
}
/*从PreResult集合构建实际结果集合时,删除所有关联键所需的此步骤在预设中用于删除冗余数据之前使用*/
$json_data=array();
foreach($info[“categorylist”]作为$key=>$value){
$products_array=array();
foreach($value[“products”]作为$product\u key=>$products){
$products_数组[]=$products;
}
$category=array();
$category[“name”]=$value[“name”];
$category[“term_id”]=$row[“term_id”];
$category[“slug”]=$row[“slug”];
$category[“products”]=$products;
$json_数据[“categorylist”][]=$category;
}
回显“”。打印($json_数据,1)。“”;
回显“”。json_编码($json_数据)。“”;
PHP代码
这在纯SQL中很难实现,因为php的mysql驱动程序不支持数组等。为了实现上面所描述的,您需要预先聚合一些中间步骤的结果,然后可以将连接的字符串连接到某个结果集(在您的案例组中)等等。现在将显示如下错误:。。。。。。。。已弃用:mysql_connect():mysql扩展已弃用,将来将被删除:请在第15行的C:\wamp\www\youtubewebservice\shop-categorylist-product.php中使用mysqli或PDO,这是一个警告。这意味着与其使用mysql_*函数,不如使用mysqli_*函数。Mysqli是与MySQL连接和交互的更新、更好的方式。大多数情况下,您可以将其与mysql_*函数一样使用。