在这里有一些人的帮助下,我一直在为我的工作网站建立一个在线目录,今天我把它从开发服务器移到生产服务器上,这样其他几个人就可以开始为我测试一些东西了。我似乎遇到了菜单查询的问题。我收到以下错误
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result,
boolean given in /websites/store/includes/menu.php
on line 15
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result,
boolean given in /websites/store/includes/menu.php on line 34
Warning: Invalid argument supplied for foreach()
in /websites/store/includes/menu.php on line 34
我花了一整天时间试图弄清楚这里发生了什么,代码在开发服务器上完美运行但只是在生产服务器上抛出错误
任何人都可以向我解释发生了什么或如何解决它
以下是页面的代码
<?php
// Category Listing From Database
// Open MySql Database Connection
include ('sqlopen.php');
// SQL Query for Category Listing
$sql = mysqli_query($conn, " SELECT CategoryName, SubcategoryName, SubcategoryID
FROM Products GROUP BY SubcategoryID ORDER BY CategoryName");
// Create Array from Data
$menu = array();
while ($row = mysqli_fetch_assoc($sql)) {
// Creates First Level Array Items For Parent IDs
if (!in_array($row['CategoryName'], $menu['CategoryName'])) {
$menu['CategoryName'][] = $row['CategoryName'];
}
if (!empty($row['SubcategoryName']))
// Creates Second Level Array for Child IDs
$menu['SubcategoryName'][$row['CategoryName']][] = $row['SubcategoryName'];
// Creates Third Level Array for Category IDs
$menu['SubcategoryID'][$row['SubcategoryName']][$row['CategoryName']][] = $row['SubcategoryID'];
}
//__________________________________________________________________________________________________________
// Category Menu
?>
<ul id="storenav">
<?php
foreach ($menu['CategoryName'] as $cat) { ?>
<li><a class="sub" tabindex="1"><?php echo $cat ?></a><ul>
<?php
foreach ($menu['SubcategoryName'][$cat] as $subcat) {
foreach($menu['SubcategoryID'][$subcat][$cat] as $id) ?>
<li><a href='/store/prodlist.php?subcatid=<?php echo $id ?>'><?php echo $subcat ?></a></li>
<?php } ?>
</ul></li>
<?php } ?>
</ul>
<?php
//__________________________________________________________________________________________________________
// Open MySql Database Connection
include ('sqlclose.php');
?>
答案 0 :(得分:1)
你假设这条线
$sql = mysqli_query()
返回结果集。但是,如果查询失败,则返回FALSE,一个布尔值。
这就是这种情况。
编写代码如下: `
if($sql = mysqli_query($conn, "your query here")){
$menu = array();
while($row = ...) {
}
}
else {
echo 'something went wrong'; // and more error handling.
}
&GT?; `
-
以及查询失败的原因? part group by在此查询中没什么关系,因为没有aggragation函数(min(),max(),count()等) 即便如此:所有列都应在“group by”部分中提及
请参阅mysqli_error()让Mysql告诉您查询失败的原因。
答案 1 :(得分:1)
首先,您没有正确处理错误。更好的说,你甚至没有检查它们。请尝试下面的代码。它将更好地描述出现了什么问题。
<?php
/**
* Created by: PhpStorm.
* Project: Stackoverflow
* File name: .php.
* User: Deathstorm
* Date: 23-6-2017.
* Time: 10:14.
* File Description: ...
*/
?>
<?php
// Category Listing From Database
// Open MySql Database Connection
include ('sqlopen.php');
// SQL Query for Category Listing
$sql = mysqli_query($conn, "SELECT CategoryName, SubcategoryName, SubcategoryID
FROM Products GROUP BY SubcategoryID ORDER BY CategoryName");
if ($conn->query($sql) === TRUE){
// Create Array from Data
$menu = array();
while ($row = mysqli_fetch_assoc($sql)) {
// Creates First Level Array Items For Parent IDs
if (!in_array($row['CategoryName'], $menu['CategoryName'])) {
$menu['CategoryName'][] = $row['CategoryName'];
}
if (!empty($row['SubcategoryName']))
// Creates Second Level Array for Child IDs
$menu['SubcategoryName'][$row['CategoryName']][] = $row['SubcategoryName'];
// Creates Third Level Array for Category IDs
$menu['SubcategoryID'][$row['SubcategoryName']][$row['CategoryName']][] = $row['SubcategoryID'];
}
}
else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
//__________________________________________________________________________________________________________
// Category Menu
?>
<ul id="storenav">
<?php
foreach ($menu['CategoryName'] as $cat) { ?>
<li><a class="sub" tabindex="1"><?php echo $cat ?></a><ul>
<?php
foreach ($menu['SubcategoryName'][$cat] as $subcat) {
foreach($menu['SubcategoryID'][$subcat][$cat] as $id) ?>
<li><a href='/store/prodlist.php?subcatid=<?php echo $id ?>'><?php echo $subcat ?></a></li>
<?php } ?>
</ul></li>
<?php } ?>
</ul>
<?php
//__________________________________________________________________________________________________________
// Open MySql Database Connection
include ('sqlclose.php');
?>