PHP MySQL UPDATE Data

Update existing records in MySQL database

MySQLi Update (Procedural)

$sql = "UPDATE users SET email='[email protected]' WHERE id=1"; # update query
if (mysqli_query($conn, $sql)) { # execute update
    echo mysqli_affected_rows($conn) . " records updated"; # count affected rows
} else { # handle error
    echo "Error: " . mysqli_error($conn); # show error
}

MySQLi Update (OOP)

$sql = "UPDATE products SET price=899.99 WHERE id=5"; # SQL update
if ($conn->query($sql) === TRUE) { # check success
    echo $conn->affected_rows . " updated"; # affected row count
}

PDO Update

$sql = "UPDATE users SET status=1 WHERE id=:id"; # prepared update
$stmt = $pdo->prepare($sql); # prepare statement
$stmt->execute(['id' => 10]); # execute with param
echo $stmt->rowCount() . " rows updated"; # count updated rows