我有这样的数据库
listing_id category_id
1 1,4,8,9
2 2,3,5,10
3 7,8,9,5,3
现在类别表是这样的
id category_name
1 apple
2 grapes
3 banana
4 pineapple
5 mango
6 strawberry
7 corn
8 leech
9 tomato
10 potato
我想创建一个完整的数组,我将从这个表中得到结果
Array
(
[0] => Array
(
[listing_id] => 1
[category_name] => apple,pineapple,leeche,tomato
)
[1] => Array
(
[listing_id] => 2
[category_name] => grapes,banana,mango,potato
)
[2] => Array
(
[listing_id] => 3
[category_name] => corn,leeche,tomato,mango,banana
)
)
所以为此,我已经制作了这样的PHP代码
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "listings";
$mysqli = new mysqli($servername, $username, $password, $dbname);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$listing_query = "SELECT * FROM `listings` ORDER BY listing_id";
$listing_array = array();
$category_array = array();
if( $result = $mysqli->query($listing_query) ) {
while( $obj = $result->fetch_object() ) {
$categories = $obj->categories;
$listing_id = $obj->listing_id;
array_push($category_array, $categories);
foreach( $category_array as $category_name ) {
$category_names = explode(',',$category_name);
$get_category_names = "SELECT * FROM `categories` WHERE `id` = $category_names";
if( $sql = $mysqli->query($get_category_names) ) {
while( $object = $sql->fetch_object() ) {
$category_name = $object->category_name;
$listing_row['category_name'] = $category_name;
}
}
}
$listing_row['listing_id'] = $obj->listing_id;
array_push($listing_array, $listing_row);
}
}
print_r($listing_array);
$mysqli->close();
但它根本不起作用.有人能告诉我如何获得像这个数组的结果
Array
(
[0] => Array
(
[listing_id] => 1
[category_name] => apple,pineapple,leeche,tomato
)
[1] => Array
(
[listing_id] => 2
[category_name] => grapes,banana,mango,potato
)
[2] => Array
(
[listing_id] => 3
[category_name] => corn,leeche,tomato,mango,banana
)
)
任何帮助和建议都会非常明显.谢谢
更新
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "listings";
$mysqli = new mysqli($servername, $username, $password, $dbname);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$listing_query = "SELECT * FROM `listings` ORDER BY `listing_id` LIMIT 5 ";
$listing_array = array();
if( $result = $mysqli->query($listing_query) ) {
while( $obj = $result->fetch_object() ) {
$categories = $obj->categories;
$listing_id = $obj->listing_id;
$listing_row['listing_id'] = $obj->listing_id;
$Query = "SELECT listing_id, group_concat(Category.category_name) category_name FROM `listings` Listing LEFT JOIN `categories` Category ON FIND_IN_SET(Category.category_id, Listing.categories) group by Listing.listing_id LIMIT 5";
if($sql = $mysqli->query($Query)){
while ($object = $sql->fetch_object()) {
$listing_row['category_name'] = $object->category_name;
}
}
array_push($listing_array, $listing_row);
}
}
print_r($listing_array);
$mysqli->close();
?>
它在每个数组块元素中显示相同的类别名称.
最佳答案 您应该能够使用group_concat来获取这样的逗号分隔列表
$get_category_names = "SELECT GROUP_CONCAT(category_name) as category_name FROM `categories` WHERE `id` = $category_names";