【问题标题】:Problem with PHP PDO fetchOjectPHP PDO fetchOject 的问题
【发布时间】:2011-09-20 20:41:24
【问题描述】:

当我在下面的查询中使用 PHP PDO fetchOject 时,这似乎是一个错误或问题,

查询:

SELECT 
    p.*,
    t.*

FROM root_pages AS p

LEFT JOIN root_templates AS t
ON p.tmp_id = t.tmp_id

WHERE p.pg_url = ?
AND ? IS NOT NULL

OR p.pg_url = ? 
AND p.pg_hide != ?

从 PHP PDO db 类调用,

$page = $this->database->fetch_object($sql,array(
            $pg_url,
            NULL,
            $pg_url,
            1
        ));

结果:

SQLSTATE[HY093]:无效的参数号:绑定变量的数量 令牌数不匹配

来自 PDO db 类的 PHP PDO FetchOject 方法,

# return the current row of a result set as an object
    public function fetch_object($query, $params = array())
    {
        try
        {
            # prepare the query
            $stmt = $this->connection->prepare($query);

            # if $params is not an array, let's make it array with one value of former $params
            if (!is_array($params)) $params = array($params);

            # execute the query
            $stmt->execute($params);

            # return the result
            return $stmt->fetchObject();
            //return $stmt->fetch(PDO::FETCH_OBJ);
        }
        catch (PDOException $e) 
        {
            # call the get_error function
            $this->get_error($e);
        }
    }

只要我这样调用方法就好了,

$page = $this->database->fetch_object($sql,array(
            $pg_url,
            1,
            $pg_url,
            1
        ));

但是当我使用phpMyAdmin 测试以下查询之一时,我可以得到没有任何错误的结果,

SELECT 
    p.*,
    t.*

FROM root_pages AS p

LEFT JOIN root_templates AS t
ON p.tmp_id = t.tmp_id

WHERE p.pg_url = 'exhibition sample 6' 
AND '1' IS NOT NULL

OR p.pg_url = 'exhibition sample 6' 
AND p.pg_hide != '1'

SELECT 
    p.*,
    t.*

FROM root_pages AS p

LEFT JOIN root_templates AS t
ON p.tmp_id = t.tmp_id

WHERE p.pg_url = 'exhibition sample 6' 
AND NULL IS NOT NULL

OR p.pg_url = 'exhibition sample 6' 
AND p.pg_hide != '1'

我在使用fetchOject 时遗漏了什么想法?

编辑:

$sql ="
SELECT 
    p.*,
    t.*

FROM root_pages AS p

LEFT JOIN root_templates AS t
ON p.tmp_id = t.tmp_id


WHERE p.pg_url = 'exhibition sample 6' 
AND ? IS NOT NULL

OR p.pg_url = 'exhibition sample 6' 
AND p.pg_hide != '1'
";

没有错误

$item = $connection->fetch_assoc($sql,1);

$item = $connection->fetch_assoc($sql,NULL);

fetch_assoc 方法,

    # fetch a single row of result as an array ( =  one dimensional array)
public function fetch_assoc($query, $params = array())
{
    try
    {
        # prepare the query
        $stmt = $this->connection->prepare($query);

        # if $params is not an array, let's make it array with one value of former $params
        if (!is_array($params)) $params = array($params);

        # execute the query
        $stmt->execute($params);

        # return the result
        return $stmt->fetch();
    }
    catch (PDOException $e) 
    {
        # call the get_error function
        $this->get_error($e);
    }


}

【问题讨论】:

    标签: php mysql sql pdo fetch


    【解决方案1】:

    您尝试执行的操作(将null 作为参数传递给execute)是不可能的。正如documentation 所说:

    输入参数

    包含与绑定参数一样多的元素的值数组 在正在执行的 SQL 语句中。 所有值都被视为 PDO::PARAM_STR.

    如果你想传入一个null,你必须用

    绑定参数
    $stmt->bindValue(1, null, PDO::PARAM_NULL);
    

    或对命名参数使用等效语法。

    【讨论】:

    • 感谢乔恩,请检查我上面的编辑。我可以通过nullfetch 而不绑定数据。怎么来的?
    • 我在课堂上发现了错误。将 null 传递给 fetchObject 没有任何问题。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 2011-02-10
    • 2017-05-18
    • 2012-07-09
    • 2016-05-19
    • 2011-09-20
    相关资源
    最近更新 更多