2011-09-20 103 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