如果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。但我需要它返回 错误 - 如果 0 rows affected. (Query took 0.0010 sec)
0 rows affected. (Query took 0.0010 sec)
是否有可能?
试试这个
return $stmt->rowCount() > 0;