2016-11-07 93 views
0

有另一個PHP問題可能是簡單的,但我真的很感謝任何幫助!PHP MySQLi問題與準備SELECT變量不綁定

我的函數執行一個mysqli計數,但它綁定到的$ count參數沒有更改,即使sql運行良好並且沒有錯誤。

功能:

function itemsCount ($conn, $list_id) { 
    $count = 0; 
    if (!($stmt = $conn->prepare("SELECT count(uid) FROM collection.user_list_item WHERE list_id = ?"))) { 
     echo "Prepare failed: " . $mysqli->error; 
    } 
    if (!($stmt->bind_param('i', $list_id))) { 
     echo "Bind failed: " . $stmt->error; 
    } 
    if (!($stmt->execute())) { 
     echo "Execute failed: " . $stmt->error; 
    } 
    if (!($stmt->bind_result($count))) { 
     echo "Bind failed: " . $stmt->error; 
    } 
    echo $count; 
    $stmt->close(); 
    return $count; 
} 

和呼叫:

public function doAdd ($conn, $list_id, $item_id) { 
    if ($this->itemsCount($conn, $list_id) < 20) { 
     ...do stuff ... 
    } 
} 

對不起代碼不是很漂亮,但請幫我指出了正確的方向!

+2

您還需要在'$ stmt-> bind_result()'之後調用'$ stmt-> fetch()'來實際檢索一行。 –

+0

謝謝@MichaelBerkowski!我現在感覺非常愚蠢...... –

回答

-1

var_dump $ list_id,param綁定我,期望一個整數。我期望你的var是一個你可以用var_dump看到的字符串。將字符串強制轉換爲int,或者使用不同的參數綁定。

+0

PHP會在必要時自動將字符串轉換爲整數。 – Barmar

+0

「bind_param」的'''參數告訴它它是必需的。 – Barmar