Mysql
 sql >> Database >  >> RDS >> Mysql

Eliminazione del record se esiste in php/PDO

Usa rowCount(); all'interno prova a controllare il numero di righe interessate dalla tua istruzione sql

try {
    $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
    /*     * * echo a message saying we have connected ** */
//echo 'Connected to database<br />';
    $sql = "DELETE FROM ukgh WHERE telephone = :telephone";
    $stmt = $dbh->prepare($sql);
    $stmt->bindParam(':telephone', $telephone, PDO::PARAM_STR);
    $stmt->execute();
    $count = $stmt->rowCount();// check affected rows using rowCount
    if ($count > 0) {
        echo 'Success - The record for ' . $telephone . ' has been deleted.';
    } else {
        echo "Your error message";
    }
} catch (PDOException $e) {
    echo $e->getMessage();
}

Leggi http://php.net/manual/en/pdostatement.rowcount.php