【问题标题】:Why is this prepared PHP PDO statement not working when the query() is?为什么这个准备好的 PHP PDO 语句在 query() 时不起作用?
【发布时间】:2013-01-28 10:07:08
【问题描述】:

我可能很笨,但我有这个函数,它根据输入计算所需的页数,从中计算需要多少页并返回。

function get_total_pages($field, $table, $page_size = 20, $where_something = "", $equals_something = ""){
    global $dbh;
    try {
        if(empty($where_something)){
        // I deleted irrelevant code here
        }
        elseif(!empty($where_something) && !empty($equals_something)){
            $count_query = $dbh->prepare("SELECT COUNT(:field) FROM :table WHERE :where=:equals");
            $count_query->bindParam(":field", $field);
            $count_query->bindParam(":table", $table);
            $count_query->bindParam(":where", $where_something);
            $count_query->bindParam(":equals", $equals_something);
            $count_query->execute();
            $count = $count_query->fetch();
            $total_records = $count[0];                 // calculating number of records in history table
            $total_pages = ceil($total_records / $page_size);   // calculating number of pages necessary
            return $total_pages;
        }
        return false;
    }
    catch(PDOException $e){
        echo $e->getMessage();
    }

我用
$total_pages = get_total_pages("username", "comments", $page_size, "username", $_GET['user']);

来称呼它

这是我得到的错误:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''comments' WHERE 'username'='redbot'' at line 1

但是,如果我将所有函数的代码换成更简单的 query() 而不是准备好的语句,它就可以工作,只要我在用户名后面加上引号:

function get_total_pages($field, $table, $page_size = 20, $where_something = "", $equals_something = ""){
    global $dbh;
    try {
        if(empty){
          //   irrelevant code
        }
        elseif(!empty($where_something) && !empty($equals_something)){
            $count_query = $dbh->query("SELECT COUNT({$field}) FROM {$table} WHERE {$where_something}={$equals_something}");
            $count = $count_query->fetch();
            $total_records = $count[0];                 // calculating number of records in history table
            $total_pages = ceil($total_records / $page_size);   // calculating number of pages necessary
            return $total_pages;
        }
        return false;
    }
    catch(PDOException $e){
        echo $e->getMessage();
    }
}

$total_pages = get_total_pages("username", "comments", $page_size, "username", "\"" . $_GET['user'] . "\"");

【问题讨论】:

    标签: php mysql pdo prepared-statement


    【解决方案1】:

    can't use dynamic field and table names in prepared statements.

    您必须自己检查它们(理想情况下,根据现有和允许的表和列名称的白名单)并自己将它们放入查询字符串中。

    Here 是一些代码 sn-ps 显示如何执行此操作。

    【讨论】:

    • 非常感谢,我可能应该已经知道这一点,但错误让我认为它与引号有关。
    • 这应该有效(使用一个绑定参数)吗? $count_query = $dbh->prepare("SELECT COUNT({$field}) FROM {$table} WHERE {$where_something}=:equals");因为它的结果是空的
    • @emik 我猜这取决于$field$table 等包含的内容?你试过输出吗?
    • 好的,现在它正在工作,但我似乎没有改变任何东西,可能与发送到主机服务器的文件有关。奇怪。无论如何,再次感谢您的帮助,很抱歉再次打扰您。
    【解决方案2】:

    不能用占位符定义列名,也可以看看bindParambindValue的区别

    一般来说,参数仅在数据操作语言 (DML) 语句中是合法的,在数据​​定义语言 (DDL) 语句中是不合法的。

    【讨论】:

    • 非常感谢!为这个问题挠头太久了。
    猜你喜欢
    • 2023-03-19
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 2015-12-07
    • 2020-08-21
    • 2018-01-24
    • 2010-11-30
    相关资源
    最近更新 更多