bind_result()-变量数与字段数不匹配 [英] bind_result() - Number of variables doesn't match number of fields

查看:43
本文介绍了bind_result()-变量数与字段数不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

// 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行

Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in C:\wamp64\www\forrrumm\view_topic.php on line 47

这:

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

Fatal error: Call to undefined method mysqli_stmt::fetch_all() in C:\wamp64\www\forrrumm\view_topic.php on line 49

推荐答案

您没有使用 bind_result()正确.

将结果集中的列绑定为变量.

Binds columns in the result set to variables.

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

You are trying to bind the entire result set into a single variable. You need to provide a variable for each column in the result set.

$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.

Alternatively, if you want to use the * in your SELECT, you could try the following non-bind_result method. (all examples that I have read online only use bind_result when not using * in the SELECT.

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']

这篇关于bind_result()-变量数与字段数不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆