您几乎将一些代码放错了位置
$host = 'localhost';
$userdb = 'root';
$senhadb = '';
$database = 'test';
$db = mysqli_connect($host, $userdb, $senhadb,$database);
function ShowSubCats($id,$db) {
$sql = "select * , (select t2.slug from menu t2 where t2.id=t1.parent) as ParentSlug from menu t1 where t1.id='$id'";
$results=mysqli_query($db,$sql);
while ($row = mysqli_fetch_assoc($results)){
$parent_Before = ShowSubCats($row['parent'],$db);
if (!empty($row['ParentSlug'])){
echo '/'.$row['ParentSlug'];
}
return $row;
}
}
$id = 6;
echo $id.':';
$sql = "SELECT * from menu where id='$id';";
$results = mysqli_query($db,$sql);
while ($row = mysqli_fetch_assoc($results)) {
$parent = ShowSubCats($row['parent'],$db);
echo '/'.$parent['label'];
}
echo '<br>';
if (!empty($parent)) print_r($parent);