【问题标题】:bind_result() - Number of variables doesn't match number of fields [duplicate]bind_result() - 变量数与字段数不匹配[重复]
【发布时间】:2017-04-07 07:57:21
【问题描述】:

代码:

// get value of id that sent from address bar
$id=filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
if ($id === false) {
    //filter failed
    die('id not a number');
}
if ($id === null) {
    //variable was not set
    die('id not set');
}

//white list table 
$safe_tbl_name = '';
switch($tbl_name){
    case 'Table1': 
        $safe_tbl_name = 'MyTable1';
        break;
    case 'Table2':
        $safe_tbl_name = 'MyTable2';
        break;
    default:
        $safe_tbl_name = 'forum_questions';
};

$sql="SELECT * FROM `$safe_tbl_name` WHERE id=?";
if ($stmt = $con->prepare($sql)){
$stmt->bind_param("s", $id);
$stmt->execute();
}
else{
   //error !! don't go further
   var_dump($con);
}
$stmt->bind_result($result);

$rows = $stmt->fetch_all(MYSQLI_ASSOC);
?>

<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="100%" border="0" cellpadding="3" cellspacing="1" bordercolor="1" bgcolor="#FFFFFF">
<tr>
<td bgcolor="#F8F7F1"><strong><?php echo $rows['topic']; ?></strong></td>
</tr>

<tr>
<td bgcolor="#F8F7F1"><?php echo $rows['detail']; ?></td>
</tr>

<tr>
<td bgcolor="#F8F7F1"><strong>By :</strong> <?php echo $rows['name']; ?> <strong>Email : </strong><?php echo $rows['email'];?></td>
</tr>

<tr>
<td bgcolor="#F8F7F1"><strong>Date/time : </strong><?php echo $rows['datetime']; ?></td>
</tr>
</table></td>
</tr>
</table>

我得到这个错误:

警告:mysqli_stmt::bind_result():绑定变量的数量不 匹配准备好的语句中的字段数 C:\wamp64\www\forrrumm\view_topic.php 第 47 行

还有这个:

致命错误:调用未定义的方法 mysqli_stmt::fetch_all() in C:\wamp64\www\forrrumm\view_topic.php 第 49 行

【问题讨论】:

  • 改变这一行 $stmt->bind_param("i", $id);到 $stmt->bind_param("s", $id);或将 $id 转换为 (int)$id
  • 为什么会有不同?
  • 是的,一个解释会很好......首先,错误发生在sql语句上方的某行......其次,$id是一个整数..他为什么要把它用作他的陈述中的字符串?我认为那不会那么好..?您可以在var_dump($id) 上查看 ID 的值,然后再将其检查为 false..?
  • 在添加 'var_dump($id);' 时得到这个C:\wamp64\www\forrrumm\view_topic.php:16:boolean false id not a number
  • 你也可以var_dump($_GET)吗?

标签: php mysqli binding prepared-statement fetch


【解决方案1】:

你没有正确使用bind_result()

将结果集中的列绑定到变量。

您正试图将整个结果集绑定到一个变量中。 您需要为结果集中的每一列提供一个变量。

$stmt->bind_result($topic,$detail,$email,$name,$datetime);

这里是适合的地方:

$sql="SELECT `topic`,`detail`,`email`,`name`,`datetime` FROM `$safe_tbl_name` WHERE id=?";
if($stmt=$con->prepare($sql)){
    $stmt->bind_param("s",$id);
    $stmt->execute();
    $stmt->bind_result($topic,$detail,$email,$name,$datetime);
    //while($stmt->fetch()){  not wrong, but not necessary to loop if only one row
    $stmt->fetch();  
        echo "<table width=\"400\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"1\" bgcolor=\"#CCCCCC\">";
            echo "<tr>";
                echo "<td>";
                    echo "<table width=\"100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" bordercolor=\"1\" bgcolor=\"#FFFFFF\">";
                        echo "<tr>";
                            echo "<td bgcolor=\"#F8F7F1\"><strong>$topic</strong></td>";
                        echo "</tr>";
                        echo "<tr>";
                            echo "<td bgcolor=\"#F8F7F1\">$detail</td>";
                        echo "</tr>";
                        echo "<tr>";
                            echo "<td bgcolor=\"#F8F7F1\"><strong>By :</strong>$name<strong>Email : </strong>$email</td>";
                        echo "</tr>";
                        echo "<tr>";
                            echo "<td bgcolor=\"#F8F7F1\"><strong>Date/time : </strong>$datetime</td>";
                        echo "</tr>";
                    echo "</table>";
                echo "</td>";
            echo "</tr>";
        echo "</table>";
    //}
    $stmt->close();
}

或者,如果您想在 SELECT 中使用 *,您可以尝试以下 non-bind_result 方法。 (我在线阅读的所有示例仅在 SELECT 中未使用 * 时才使用 bind_result。

if($stmt->execute()){
    $result=$stmt->get_result();
    $rows[]=$result->fetch_assoc();
}else{
    echo "execute failed";  // but I don't think this is your problem
}
// $rows['topic']
// $rows['detail']
// $rows['email']
// $rows['name']
// $rows['datetime']

【讨论】:

  • 我收到这个错误 atm: mysqli_stmt::bind_result(): Number of bind variables does not match number of fields in Prepared statement in C:\wamp64\www\forrrumm\view_topic.php 在线47
  • 还有这个:致命错误:在第 49 行调用 C:\wamp64\www\forrrumm\view_topic.php 中未定义的方法 mysqli_stmt::fetch_all()
  • 当我这样做时我得到 29,如果我点击 id 为 28 的主题,我得到 28
  • 我尝试了现在执行下的代码,现在它似乎工作得更好了。它从我的数据库中回显信息,但我收到错误消息,告诉我“主题”是一个身份不明的索引,“详细信息、电子邮件、名称和日期时间”也是如此
  • 好的,我应该把这个放在代码的什么地方?:$stmt->bind_result($topic,$detail,$email,$name,$datetime);
猜你喜欢
  • 2016-12-28
  • 1970-01-01
  • 2014-01-06
  • 1970-01-01
  • 2020-02-05
  • 2012-09-24
  • 1970-01-01
相关资源
最近更新 更多