簡體   English   中英

PHP MySQLi准備的語句獲取-第一行為空?

[英]PHP MySQLi prepared statement fetch - first row empty?

如下代碼:

        $con = ConnectDB::getConnection();
    if ($stmt = $con->prepare('---SQL here with 1 param---')) {
        $stmt->bind_param('i', $this->id);
        $stmt->execute();
        $stmt->bind_result($device_id, $device_identifier);

        while ($stmt->fetch()) {
            $device = new Device($device_identifier, $device_id);
            $all_devices[] = $device;
        }

獲取空結果? 第一次循環執行時,$ device_id和$ device_identifier為空,但是fetch()方法返回true,因此它可以運行。 但是,第二次迭代實際上包含了我的結果。

我可以簡單地檢查它們是否為空並忽略它們,但是我真的不明白為什么它返回一個空對? 我嘗試直接在數據庫上運行SQL,它只返回1個結果行?

有任何想法嗎?

您可能會想到,MySQLi准備好的語句訪存不會返回空的第一行。 因此,這種行為幾乎沒有意義。

您需要學習為斷言創建正確的證明。
您發布的代碼不正確。 由於某種原因,您試圖通過間接后果來判斷問題。 為什么不檢查問題本身呢?

    $con = ConnectDB::getConnection();
if ($stmt = $con->prepare('---SQL here with 1 param---')) {
    $stmt->bind_param('i', $this->id);
    $stmt->execute();
    $stmt->bind_result($device_id, $device_identifier);

    $stmt->fetch();
    var_dump($device_identifier, $device_id);

-如果您對空白結果的假設是對還是錯,此代碼將明確告訴您。

了解到這一點之后,您可以開始調試 ,以在您自己的代碼中找到造成當前行為的位置。

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM