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);
}
}
感謝喬恩,請檢查我上面的編輯。我可以通過'fetch'傳遞'null'而不綁定數據。怎麼來的? – laukok
我在班上發現了這個錯誤。將null傳遞給fetchObject沒有任何問題。謝謝您的幫助。 – laukok