2011-09-20 104 views
0

这似乎是一个错误或问题,当我使用PHP PDO fetchOject与下面的查询,问题与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 [HY 093]:无效的参数编号:绑定变量的数量 不匹配令牌

PHP从PDO DB类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); 
    } 


} 

回答

0

您正在尝试做什么(通过null作为参数到execute)是不可能的。作为documentation状态:

input_parameters

值的数组具有一样多的元素就必然有正在执行参数 在SQL语句。 所有数值被视为 PDO :: PARAM_STR

如果你想在一个null传递,您必须将参数与

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

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

+0

感谢乔恩,请检查我上面的编辑。我可以通过'fetch'传递'null'而不绑定数据。怎么来的? – laukok

+0

我在班上发现了这个错误。将null传递给fetchObject没有任何问题。谢谢您的帮助。 – laukok