Strange question, but I can not find. How in PDO to check whether the request to the database was successful?
$result = $this->db->query($q); Returns Object always.

 public function query($sql, $params = []) { $stmt = $this->db->prepare($sql); if (!empty($params)) { foreach ($params as $key => $val) { $stmt->bindValue(':'.$key, $val); } } $stmt->execute(); return $stmt; } 

  • one
    The dock says: Return values ​​PDO :: query () returns a PDOStatement object or FALSE if the query could not be completed - Edward

2 answers 2

The comments have already said that in case of an error, false is returned, but as an option you can see how many lines affected the request:

 $stmt->execute(); if($stmt->rowCount() > 0) { // запрос удался } else { // запрос по каким-то причинам не выполнен } 
  • Thank. I'm not used to PDO yet - Andrii
  if ($result && !is_string($result)) { //return true если успешно //код } 
  • He is always true. Even if there is no field in the database and so on. The $ result line comes with the query - Andrii
  • @Andrii is natural, because you are returning the result of prepare (the preparation of the query), and not the execute (execution). Although in an amicable way, the answer is really not said. - D-side