亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

mysqli_stmt_bind_result() 的參數計數錯誤

mysqli_stmt_bind_result() 的參數計數錯誤

PHP
富國滬深 2023-09-08 10:22:44
我的托管公司不提供 MySQLnd,所以我必須更改代碼中的一些內容。這是我的代碼:<?phpinclude_once 'db.php';$sql = "SELECT * FROM table1 ORDER BY id DESC";$stmt = mysqli_stmt_init($connect);if (!mysqli_stmt_prepare($stmt, $sql)) {    echo "SQL statement failed";} else{    mysqli_stmt_execute($stmt);    $result = mysqli_stmt_get_result($stmt);    while ($row = mysqli_fetch_assoc($result)) {    ...當我改變它$result = mysqli_stmt_get_result($stmt);給$result = mysqli_stmt_bind_result($stmt);我Warning: Wrong parameter count for mysqli_stmt_bind_result()和Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given我已經為此工作了兩天,但由于我是初學者,所以無法解決。我應該怎么辦?
查看完整描述

1 回答

?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

請耐心聽我說,因為自從我使用程序 mysqli 以來已經很久了......


如果您希望動態綁定列參數并執行獲取循環(因為 mysqlnd 不是一個選項),那么這里的代碼可以實現這一點。


mysqli_stmt_execute($stmt);


// now before you loop on $stmt->fetch, you must bind all the columns that exist

// to a row which will hold the values during the looping on fetch (yes, confusing)


$row    = array();  // will hold each fetch'd loop result

$params = array();  // something to pass keyed references to $row with

$params[] = $stmt;  // add the $stmt object as first param (for procedural way)


$meta = mysqli_stmt_result_metadata($stmt);// get what those columns will be


while($field = $meta->fetch_field()) {

    if (!isset($row[ $field->name ])) { $row[ $field->name ] = null; } // set if not set

    $params[] = &$row[ $field->name ]; // add reference to keyed row value

}


$meta->close();// metadata no longer needed, close it


call_user_func_array('mysqli_stmt_bind_result', $params);


while (mysqli_stmt_fetch($stmt)) {


    // in here you then have $row to use as before

    echo $row['id'];


}

現在,你可能會認為我在這里太過分了……是的。該示例用于處理未知的sql 列抓?。ㄊ褂肧ELECT *語法)。但是,如果您知道所有列都出來,則可以簡單地在 while 循環之前綁定每一列,如下所示:


mysqli_stmt_execute($stmt);

mysqli_stmt_bind_result($stmt, $id, $col2, $col3);


while (mysqli_stmt_fetch($stmt)) {


    // in here you then use $id, $col2, $col3

    echo $id;


}

這就是為什么切換到該PDO庫可以使事情變得非常容易,因為它提供了在簡單循環中簡單地獲取具有關聯鍵名的行的規定,就像您所知道并喜歡的可用一樣mysqlnd。然而,如果PDO這也不是一個選項,那么您將面臨痛苦且神秘的方法來處理“binds”和“mysqli”。


查看完整回答
反對 回復 2023-09-08
  • 1 回答
  • 0 關注
  • 171 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號