我正在尝试使用数据库表中的数据制作菜单。但我有一个小问题。我的查询结果返回一个错误的布尔值。
这是我的代码:
<?php
class DataBase
{
protected $dbUser;
protected $dbPassword;
protected $dbHost;
protected $dbName;
protected $db;
function __construct()
{
$this->dbUser = 'root';
$this->dbPassword = '';
$this->dbHost = 'localhost';
$this->dbName = 'ecommercemakup';
$this->db = mysqli_connect($this->dbHost, $this->dbUser, $this->dbPassword, $this->dbName) or die('Fatal error!');
}
public function getInstance()
{
return $this->db;
}
}
?>
<?php
$db = new DataBase();
$r = mysqli_query($db->getInstance(), "CALL categories()");
if (mysqli_num_rows($r) > 0) {
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo '<li> <a href=' . $row['name'] . '.php>';
print_r($row['name']);
echo '</a>
<ul>';
$id = intval($row['idCategory']);
$r1 = mysqli_query($db->getInstance(), "CALL subcategories($id)");
if (mysqli_num_rows($r1) > 0) {
while ($row1 = mysqli_fetch_array($r1, MYSQLI_ASSOC)) {
echo '<li><a href=' . $row1['name'], '.php>';
print_r($row1['name']);
echo '</a></li>';
}
}
echo '</ul></li>';
}
}
?>
第一个查询运行正确,但第二个查询总是返回 false。
这两个程序是这样的:
DELIMITER @@
DROP PROCEDURE categories @@
CREATE PROCEDURE ecommercemakup.categories()
BEGIN
SELECT * FROM categories;
END @@
DELIMITER ;
DELIMITER @@
DROP PROCEDURE subcategories @@
CREATE PROCEDURE ecommercemakup.subcategories
(IN id_Category INT)
BEGIN
SELECT idSubcategory, idCategory, name FROM subcategories WHERE idCategory = id_Category;
END @@
DELIMITER ;
该categories
表包含 2 个项目:idCategory
和name
,并且该subcategories
表包含 3 个项目idSubcategory
:idCategory
和name
。
有人可以告诉我我在这里做错了什么吗?