我一直在使用这段代码,而且它一直运行良好,但现在我提出的查询只返回一个结果,突然$findproductsrow
为NULL
。我该如何解决这个问题? (第一个$ findproductsresult不为null,第二个是。)我是否需要添加一个子句,只返回一行?
$UserID = $_SESSION['userID'];
$findproductsstmt = mysqli_prepare($connection, "SELECT DISTINCT PID FROM prices WHERE UserID = ? and Manu = 0");
mysqli_stmt_bind_param($findproductsstmt, 'i', $UserID);
mysqli_stmt_execute($findproductsstmt);
$findproductsresult = mysqli_stmt_get_result($findproductsstmt);
if(mysqli_num_rows($findproductsresult) == 0) {
echo('This user is not retailing any products.');
}
else{
$findproductsrow = mysqli_fetch_array($findproductsresult, MYSQLI_ASSOC);
var_dump($findproductsresult);
while($findproductsrow = mysqli_fetch_array($findproductsresult, MYSQLI_ASSOC)){
$_SESSION['PID'] = $findproductsrow['PID'];
include '../includes/baseproductlist.php';
}
}
答案 0 :(得分:1)
您的问题出在ELSE
声明中。
<强>问题强>
else{
$findproductsrow = mysqli_fetch_array($findproductsresult, MYSQLI_ASSOC); // this pulls the first record, remove this
var_dump($findproductsresult);
while($findproductsrow = mysqli_fetch_array($findproductsresult, MYSQLI_ASSOC)){ // causing this to try and pull the second record which doesn't exist
$_SESSION['PID'] = $findproductsrow['PID'];
include '../includes/baseproductlist.php';
}
}
<强>解决方案强>
else{
while($findproductsrow = mysqli_fetch_array($findproductsresult, MYSQLI_ASSOC)){
$_SESSION['PID'] = $findproductsrow['PID'];
include '../includes/baseproductlist.php';
}
}