如果0行受PDO影响,我如何返回false ?
我有这个方法来执行SQL查询,
public function executeSQL($query, $params = array())
{
try
{
$stmt = $this->connection->prepare($query);
$params = is_array($params) ? $params : array($params);
$stmt->execute($params);
return true;
}
catch (PDOException $e)
{
// call the get_error function
$this->get_error($e);
}
}
所以我可以像下面这样运行查询来更新表中的一行,
$update = "
UPDATE content_has_language
SET text = ?
WHERE content_id ?
AND language_id IN
(
SELECT language_id
FROM language AS l
WHERE l.code = ?
)
";
$result = $this->connection->executeSQL($update,array(
'xxx',
'1',
'en'
));
无论行是否匹配和更新,它总是返回true。但是我需要它返回false - if 0 rows affected. (Query took 0.0010 sec)
有可能吗?
试试这个
return $stmt->rowCount() > 0;
请考虑
return $stmt->rowCount() > 0;
或
return (bool) $stmt->rowCount();