我有一张表categories
就像这样
CREATE TABLE categories (
id int not null primary key AUTO_INCREMENT,
main_name varchar(64)
sub_name varchar(64)
)
现在我希望获取所有类别和子类别以获取侧栏:
<?php
$query = "SET names 'utf8'";
$send_query = mysqli_query($connection, $query);
$query = "SELECT main_name FROM categories";
$result = mysqli_query($connection, $query);
$array = array();
while($row = mysqli_fetch_array($result)) {
if ( !in_array($row['main_name'], $array)) {
echo "<a href='' class='list-group-item'>{$row['main_name']}</a>";
array_push($array, $row['main_name']);
$query = "SELECT sub_name FROM categories WHERE main_name=".$row['main_name'];
$inner_query_result = mysqli_query($connection, $query);
if($inner_query_result !== FALSE) {
while ($inner_row = mysqli_fetch_array($inner_query_result)) {
echo $inner_row['sub_name'];
}
}
}
}
?>
但是我没有获得子类别,因为第二个查询失败了,不知道为什么。如何正确获取所有子类别?
答案 0 :(得分:0)
$query = "SELECT sub_name FROM categories WHERE main_name='".$row['main_name']."'";