首先让我说我的SQL是正确的并且多次检查。
我的文件中有两个方法为SQL调用多次,但是当涉及到第二个部分时,它不会返回任何值来填充字段。
以下是给我提出问题的部分:
$query = mysql_query("SELECT * FROM MegaFilter WHERE Parent = '".$ThisPage."' ");
while($query2 = mysql_fetch_assoc($query)) {
$SubID = $query2['Filter'];
$query3 = mysql_query("SELECT * FROM SubCategories WHERE ID = '".$SubID."' ");
while($query4 = mysql_fetch_assoc($query3)) {
$SubCatID = $query4['ID'];
$query5 = mysql_query("SELECT * FROM Web_Pages WHERE SubCat = '".$SubCatID."' ");
}
while($query6 = mysql_fetch_assoc($query5)) {
$ProductName = $query6['Title'];
$ProductID = $query6['ID'];
}
echo '<li class="item" data-id="id-'.$Productid.'" data-type="'.$SubID.'">'.$Productname.'</li>';
}
除了未定义最后2个变量外,它不会记录任何错误。
答案 0 :(得分:1)
Variable names在php中区分大小写。您正在为$ProductName
和$ProductID
分配值,但您使用的是$Productid
(小写i)和$Productname
(小写n)。
答案 1 :(得分:0)
我认为您需要的代码是(只需要一个select语句):
$connection = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
$stmt = $connection -> mysqli_prepare('SELECT m.Filter AS SubID, w.Title AS ProductName, w.ID AS ProductID FROM MegaFilter AS m, INNER JOIN SubCategories AS sc ON (sc.ID = m.Filter) INNER JOIN Web_Pages AS w ON (w.SubCat = sc.ID) WHERE Parent = ?');
$stmt->bind_param('s', $ThisPage);
$stmt->bind_result($SubID, $ProductName, $ProductID);
$stmt->execute();
while ($stmt->fetch())
{
echo '<li class="item" data-id="id-'.$ProductID.'" data-type="'.$SubID.'">'.$ProductName.'</li>'
}