dpicx06888 2017-04-07 07:57
浏览 346
已采纳

bind_result() - 变量数与字段数不匹配

Code:

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

And I get this error:

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

And this:

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

  • 写回答

1条回答 默认 最新

  • doudonglu3764 2017-04-07 09:48
    关注

    You are not using bind_result() properly.

    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);
    

    Here is where it fits in:

    $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();
    }
    

    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']
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要