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