2017-09-07 34 views
-2

蔭收到此錯誤爲什麼iam在error_log文件中得到與bind_result相關的錯誤以及如何解決?

[07月 - 2017年11點48分47秒UTC] PHP的警告:mysqli_stmt :: bind_result(): 綁定變量的數量不匹配字段數準備 聲明

$stmt = $con->prepare("SELECT * FROM table where Id =?"); 
$stmt->bind_param("s", $_POST['Id']); 
$stmt->execute(); 
$result = $stmt-> bind_result($Id); 
$numRows = $result->num_rows; 
if($numRows > 0) { 
if($row = $result->fetch_assoc()) 
{ 
$Taxname=$row['TaxName']; 
$Tid=$row['Id']; 
}} 
+0

您在'$ Id'中獲得的價值是多少? – Gunaseelan

+0

@Gunaseelan sir .i只需要知道bind_result中的變量應該是什麼?數值來自數據庫嗎? – krish

+0

你在表名'table'中得到了多少列和什麼是列值? – Gunaseelan

回答

0

您需要修改您的查詢。而不是*您需要選擇從數據庫中挑選出您實際需要的數據。
例如,如果表table有列Id,TaxName那麼你將執行就像這樣:

<?php 
$sqlData = array(); 
$stmt = $con->prepare("SELECT Id,TaxName FROM table where Id =?"); 
$stmt->bind_param("i", $_POST['Id']); //id is an integer? this should be i instead of s 
$stmt->execute(); 
$stmt->store_result(); //store the result, you missed this 
$stmt-> bind_result($Id,$TaxName); //bind_result grabs the results and stores in a variable 
$numRows = $stmt->num_rows; //see the correction I made here 
if($numRows >0){ 
     while ($stmt->fetch()) { //propper way of looping thru the result set 
      $sqlData [] = array($Id,$TaxName); 
      //assoc array would look like: 
      //$sqlData [] = array("Id" =>$Id,"TaxName" => $TaxName); 
     } 
} 
$stmt->close(); //close the connection 
?> 

然後,你將有你與mysqli的查詢結束後,您可以使用結果的數組。 希望這可以幫助你更多地理解它。

0
$stmt = $con->prepare("SELECT Id,TaxName FROM table where Id =?"); 
$stmt->bind_param("s", $_POST['Id']); 
$stmt->execute(); 
$result = $stmt-> bind_result($Id,$TaxName); 
$stmt->store_result(); 
$numRows = $stmt->num_rows; 
if($numRows > 0) { 

    while($result->fetch_assoc()) 
    { 
    $newid = $Id; 
    $newtaxname= $TaxName; 
    } 
    print_r($newid)."<br>"; 
    print_r($newtaxname); 
} 

這個代碼將會給你答案沒有任何警告。

參考:http://php.net/manual/en/mysqli-stmt.bind-result.php

mysqli_stmt :: bind_result - 綁定變量到準備好的語句 結果存儲

+0

'$ result = $ stmt-> bind_result($ Id,$ TaxName);'從文檔中,'bind_result'返回'TRUE'或'FALSE'。他會得到沒有錯誤/警告,因爲'$ numRows'將始終返回0 – IsThisJavascript

+0

@ WillParky93我已經更新答案爲'$ stmt-> num_rows'而不是'$ result-> num_rows'。 – Gunaseelan

+0

請編輯您的答案,在'$ stmt-> num_rows;'之前包含'$ stmt-> store_result();'因爲我們正在做一個'prepare'而不是'query','num_rows'不喜歡準備那麼多!在這種情況下,'num_rows'將返回0或不正確的數字 – IsThisJavascript

相關問題