【问题标题】:PHP prepared statement error: Warning: mysqli_stmt::bind_param():PHP 准备语句错误:警告:mysqli_stmt::bind_param():
【发布时间】:2013-08-17 20:45:49
【问题描述】:

我正在学习使用准备好的语句从我的数据库中的表中选择我的所有数据,但是我收到了这个错误

Warning: mysqli_stmt::bind_param(): Number of variables doesn't match 
number of parameters in prepared statement in 
/Applications/XAMPP/xamppfiles/htdocs/contenteditable/classes/class.Insert.inc on 
line 13

也许我没有以正确的方式使用准备好的语句,我不确定,我以前使用过准备好的语句,所以希望有人能告诉我我哪里出错了,或者是否有人有一个有用的示例。

这是我的代码:

index.php

<div id="maincontent" contenteditable="true">
    <?php
        //get data from database.
        require("classes/class.Insert.inc");
        $insert = new Insert();
        $insert->read();
     ?>

    <button id="save">Save</button>
        <input type="button" id="clear" value="Clear changes" />

    </div>

类/class.Insert.php

<?php 
include("connect/class.Database.inc");

 class Insert extends Database {

    public $firstname;
    public $content;

public function read(){

    $stmt = $this->mysqli->prepare('SELECT * FROM datadump');
    $stmt->bind_param('s', $content);

    $stmt->execute();

    $result = $stmt->get_result();
    while ($row = $result->fetch_assoc()) {
        echo $content;      }
        }           
  }
 ?>

【问题讨论】:

    标签: php mysqli prepared-statement


    【解决方案1】:

    bind_param 为您的查询绑定 参数,该查询有零个占位符。这会导致 PHP 抱怨。

    您可能打算改用bind_result,这是将结果集数据导出到变量的方法。

    【讨论】:

    • 当我将其更改为 bind_result 时,我收到以下错误 Fatal error: Cannot pass parameter 1 by reference in /Applications/XAMPP/xamppfiles/htdocs/contenteditable/classes/class.Insert.inc on line 13 @Jon
    • @gutigrewal:你读过bind_result 的手册吗?它不需要's' 参数,因为这没有任何意义(数据库已经知道列的数据类型)。毫不奇怪,盲目地将一个函数的名称替换为另一个函数的名称并不能神奇地使事情奏效。
    • Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in /Applications/XAMPP/xamppfiles/htdocs/contenteditable/classes/class.Insert.inc on line 15 现在当我取出s @jon 时出现此错误
    • @gutigrewal:是的,因为您使用* 选择SQL 查询中的所有列并且只绑定一个。如果您不想要任何其他内容,那么SELECT 只需一列。错误信息很清楚。
    • 我已经将它更新到数据库中的所有字段,但是使用上面 index.php 中的代码它没有回显我的数据,我没有正确调用该函数吗?我没有错误消息,所以我认为没问题我尝试了 echo $content;在 index.php 但我有一个未定义的变量@jon
    猜你喜欢
    • 1970-01-01
    • 2015-05-17
    • 2013-05-27
    • 2013-12-30
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    • 2016-07-19
    相关资源
    最近更新 更多