为什么有时仅收到“未捕获的错误:在bool上调用成员函数bind_param()”?

时间:2019-11-28 10:52:26

标签: php mysqli

我正在执行mysqli语句,但出现此错误,但我不知道为什么。我正在执行两个语句,第一个有效,但是第二个无效,它们几乎相同。在第二个错误中,我得到了这个错误:

  

未捕获的错误:在bool上调用成员函数bind_param()

代码:

$bestid = 5;
    if ($bestid != 1) {
        $idPrev = $bestid - 1;

        $stmt1 = $con->prepare("SELECT model, avgFlow FROM item WHERE "
                . "id = ?");


        $stmt1->bind_param('i', $idPrev);


        $stmt1->execute();


        $stmt1->bind_result($modelPrev, $avgPrev);
        $stmt1->fetch();

    }
    if ($bestid != 41) {
        $idNext = $bestid + 1;

        $stmt2 = $con->prepare("SELECT model, avgFlow FROM item WHERE "
                . "id = ?");


        $stmt2->bind_param('i', $idNext);


        $stmt2->execute();


        $stmt2->bind_result($modelNext, $avgNext);
        $stmt2->fetch();
    }

编辑:如果我删除第一个,而仅执行第二个,则可以,但是我不知道为什么它不能用于两个语句

EDIT2:我已经解决了这个问题:

工作代码:

$bestid=5;
 if ($bestid != 1) {
        $idPrev = $bestid - 1;
    }
    if ($bestid != 41) {
        $idNext = $bestid + 1;
    }
    if ($idNext != 0 && $idPrev != 0) {

        $stmt1 = $con->prepare("SELECT id, model, avgFlow FROM item WHERE "
                . "id = ? || id = ?");

        $stmt1->bind_param('ii', $idPrev, $idNext);
        $stmt1->execute();

        $stmt1->bind_result($id2, $model2, $avg2);
        $stmt1->fetch();
        }
    } elseif ($idNext == 0 && $idPrev != 0) {

        $stmt1 = $con->prepare("SELECT model, avgFlow FROM item WHERE "
                . "id = ?");
        $stmt1->bind_param('i', $idPrev);

        $stmt1->execute();

        $stmt1->bind_result($id2, $model2, $avg2);
        $stmt1->fetch();
    } elseif ($idNext != 0 && $idPrev == 0) {
        $stmt1 = $con->prepare("SELECT model, avgFlow FROM item WHERE "
                . "id = ?");

        $stmt1->bind_param('i', $idNext);


        $stmt1->execute();


        $stmt1->bind_result($id2, $model2, $avg2);
        $stmt1->fetch();
    }

0 个答案:

没有答案