douzhai7873 2011-09-20 20:41
浏览 39
已采纳

PHP PDO fetchOject的问题

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


}
  • 写回答

1条回答 默认 最新

  • dqjgf0982 2011-09-20 22:31
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析