2013-03-24 26 views
-2

我只是想從數據庫中選擇一幫領域的 - 因爲我已經做了很多次才......但不知何故,我得到這個錯誤:mysqli_stmt_bind_result()變量的數目不匹配?幫我怎麼算

Warning: mysqli_stmt_bind_result(): Number of bind variables doesn't match number of fields in prepared statement

但我計算正好14列,所以爲什麼當我添加14個變量它拋出這個錯誤?

public function get_invitation_fields() 
{ 
    $this->fields_db = array(); 
    include('system/mysqli_db.php'); //db connection opens here 
    $statement="SELECT 
    invitation_ID, 
    recipient, 
    text, 
    name, 
    usr_ID, 
    deleted, 
    send_date, 
    resend_date, 
    last_date, 
    status, 
    register_date, 
    verify_date, 
    redeem_date 
    trans_ID 
    FROM invitations WHERE email=?"; 

    if ($stmt = mysqli_prepare($db, $statement)) 
    { 
    mysqli_stmt_bind_param($stmt, "s", $this->email); 

    if(!mysqli_stmt_execute($stmt)) 
    {echo mysqli_stmt_error($stmt); echo mysqli_error($db); } 

    mysqli_stmt_bind_result($stmt, 
    $this->fields_db['invitation_ID'], 
    $this->fields_db['recipient'], 
    $this->fields_db['text'], 
    $this->fields_db['name'], 
    $this->fields_db['usr_ID'], 
    $this->fields_db['deleted'], 
    $this->fields_db['send_date'], 
    $this->fields_db['resend_date'], 
    $this->fields_db['last_date'], 
    $this->fields_db['status'], 
    $this->fields_db['register_date'], 
    $this->fields_db['verify_date'], 
    $this->fields_db['redeem_date'], 
    $this->fields_db['trans_ID'] 
    ); //PHP points the error to this line. 

    mysqli_stmt_fetch($stmt); 

    $this->invite_fields_db = $this->fields_db; 

    mysqli_stmt_close($stmt); 
    } 
    else 
    { 
    echo mysqli_stmt_error($stmt); 
    echo mysqli_error($db); 
    } 
    mysqli_close($db);   
} 

任何人都可以看到有什麼問題嗎?

回答

0

只是不要使用mysqli和它的bind_result,這確實會讓你要求其他人計算你的變量。

要麼使用PDO,這將使你的代碼越短

public function get_invitation_fields($email) 
{ 
    global $pdo; // connection should be opened ONCE at the beginning of the whole app 

    $sql = "SELECT * FROM invitations WHERE email=?"; 
    $stm = $pdo->prepare($sql); 
    $stm->execute(array($email)); 
    return $stm->fetch(); // values have to be RETURNED, not assigned 
} 

或至少使用get_result()來獲取從查詢一個熟悉的陣列,而不需要每個變量的自動綁定,雖然它不是保證工作。

相關問題