0

我一直在使用这段代码,它运行良好,但现在我输入了一个只返回一个结果的查询,突然$findproductsrow就变成了NULL. 我该如何解决这个问题?(第一个 $findproductsresult 不为空,第二个为空。)如果只返回一行,我是否需要添加一个子句?

$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';
    }
}
4

1 回答 1

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';
    }
}
于 2013-09-17T16:54:13.560 回答