PHP PDO fetchOject 的问题

发布于 2024-12-05 17:58:21 字数 3433 浏览 0 评论 0原文

当我将 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 数据库类调用,

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

结果:

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

PHP PDO FetchOject 方法来自 PDO db 类,

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


}

It seems to be a bug or problem when I use PHP PDO fetchOject with the query below,

Query:

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 != ?

called from a PHP PDO db class,

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

result:

SQLSTATE[HY093]: Invalid parameter number: number of bound variables
does not match number of tokens

PHP PDO FetchOject method from the PDO db class,

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

It only will be fine if I call the method like this,

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

But I can get the result without any error when I test one of the query below with 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'

or

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'

Any ideas what I have missed when using fetchOject?

EDIT:

$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'
";

No error with

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

or

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

the fetch_assoc method,

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


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独孤求败 2024-12-12 17:58:22

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

输入参数

值的数组,其元素数量与绑定参数的数量相同
在正在执行的 SQL 语句中。 所有值都被视为
PDO::PARAM_STR

如果要传入 null,则必须将参数与

$stmt->bindValue(1, null, PDO::PARAM_NULL);

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

What you are trying to do (pass in null as a parameter to execute) is not possible. As the documentation states:

input_parameters

An array of values with as many elements as there are bound parameters
in the SQL statement being executed. All values are treated as
PDO::PARAM_STR
.

If you want to pass in a null, you have to bind the parameters with

$stmt->bindValue(1, null, PDO::PARAM_NULL);

or use the equivalent syntax for named parameters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文