2017-09-07 32 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

相关问题