If we do not expect to get a result from the query we will make, we should use the “exec” method, if a result is desired, the query method. In summary; “Query” for our queries such as “DELETE / UPDATE / INSERT”, “query” for our queries such as “SELECT”.
<?php
try {
$db = new PDO('mysql:host=localhost;dbname=test', $user, $password);
$db->exec('SET NAMES `UTF-8`');
$count = $db->exec('DELETE FROM messages WHERE old = 1');
echo $count . ' messages deleted';
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
The Exec method returns the number of rows it affects as a result.