【问题标题】:PHP bind_param(): Number of variables doesn't match number of parameters in prepared statementPHP bind_param():变量数与准备好的语句中的参数数不匹配
【发布时间】:2013-04-27 07:31:19
【问题描述】:

我一直在寻找,但我没有找到问题,希望你能帮助我:

“警告:mysqli_stmt::bind_param(): 变量数不匹配 第 24 行“mydir”中准备好的语句中的参数数”

<?php
class Tunier {

    // name of Tunier
    protected $tunier_name;
    // count of participants
    protected $participant_count;

    function __construct($name, $participant) {
        // mysqli connection
        $db = new mysqli('localhost','root','','tuniere') or die ('Cant connect to MySql Please try later again');

        $this->tunier_name = $name or die('---');
        $this->participant = $participant or die('---');

        // einrichtung der Tabelle
        $query = 'CREATE TABLE `?`(
                                                    `?` VARCHAR(32) PRIMARY KEY NOT NULL,
                                                    `?` INT(2) NOT NULL,
                                                    `id_spieler` INT(10) NULL                            
                                                    );';

        $stmt = $db->prepare($query);        
        $stmt->bind_param('sss', $this->tunier_name, $this->tunier_name, $this->participant_count);
        $stmt->execute();
    }
}
?>

【问题讨论】:

    标签: php mysql oop


    【解决方案1】:

    恐怕您误用了准备好的语句功能。绑定参数旨在将数据注入您的查询(数字、文字字符串...),而不是注入运算符、标识符或关键字。如果您接受来自不受信任来源的 SQL 命令,则无法防止 SQL 注入。换句话说:您不能将表名或列名作为绑定参数传递。

    SQL 解析器不只是盲目地替换所有? 符号——它是一个上下文相关的工具。因此,这:

    `?`
    

    ... 不被视为占位符,而是文字标识符,因此是错误的。

    例如,在以下代码中:

    SELECT *
    FROM `?`
    WHERE foo=? OR bar='?' OR dot='How are you doing?'
    

    ...我们最多有? 符号,但解析器只会将foo=? 处的符号视为占位符,因为当代码被解析为SQL 时,? 是唯一一个位于文字值的位置预计。所以:

    1. 代码只有在传递一个参数时才会运行。
    2. 如果您没有名为 ? 的表,则查询填充失败。

    【讨论】:

    • 我想我真的不明白你的说法,我只是在我的 uery 中注入数据?
    • @user2316478 我将使用不同的词:您不能将表名或列名作为绑定参数传递。
    • 啊好吧 :),我想当我使用 INSERT INTO 语句插入数据时我会喜欢
    【解决方案2】:

    你的table_name不需要bind_param()

    在bind_param() 中将Integer type 更改为participant_count

    $stmt->bind_param('ssi', $this->tunier_name, $this->tunier_name, $this->participant_count);
    

    【讨论】:

      猜你喜欢
      • 2022-05-27
      • 2023-03-27
      • 2016-05-02
      • 2017-10-17
      • 2022-12-21
      • 2013-01-24
      • 2016-01-23
      • 2015-05-17
      • 2013-05-27
      相关资源
      最近更新 更多