PHP MySQL Update and Delete Data
Update Data In a MySQL Table Using MySQLi and PDO
To change existing records in a table, use the UPDATE
statement.
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
Take note of the WHERE
clause in the UPDATE
syntax : The WHERE
clause defines which record or records should be modified. If you leave out the WHERE clause, all records will be updated!
Let's look at the "Employee" table :
ID | Name | City |
---|---|---|
1 | Suresh | Paris |
2 | Babu | London |
3 | Kumar | New York |
Example (MySQLi Object-oriented)
The affected_rows
property is used to return number of rows affected or changed by last executed query (create, insert, update, and delete).
In the "Employee" table, the following examples update the record for city column with the id=3 value :
connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Build sql update query
$sql = "UPDATE Employee SET city='Paris' WHERE id=3";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully
";
$count = $conn->affected_rows;
echo "Total updated records : " . $count;
} else {
echo "Error: " . $sql . "
" . $conn->error;
}
// Close connection
$conn->close();
?>
Output:-
Total updated records : 1
After executing the above program the "Employee" table look like:
ID | Name | City |
---|---|---|
1 | Suresh | Paris |
2 | Babu | London |
3 | Kumar | Paris |
Example (MySQLi Procedural)
The mysqli_affected_rows()
method is used to get the number of records affected by last executed query.
query($sql) === TRUE) {
echo "Record updated successfully
";
$count = mysqli_affected_rows($conn);
echo "Total updated records : " . $count;
} else {
echo "Error: " . $sql . "
" . mysqli_error($conn);
}
// Close connection
mysqli_close($conn);
?>
Example (PDO)
The prepare()
method is used to build a sql query.
The execute()
method is used to execute the prepared sql query.
The rowCount()
funtion return the number of affected records in the table by last executed query.
prepare($sql);
// Execute query
$st->execute();
// Display updated message
echo "Record updated successfully
";
$count = $st->rowCount();
echo "Total updated records : " . $count;
} catch(PDOException $e) {
echo $sql . "
" . $e->getMessage();
}
$conn = null;
?>
Related Links
Delete Data From a MySQL Table Using MySQLi and PDO
To remove records from a table, use the DELETE
statement :
DELETE FROM table_name
WHERE some_column = some_value
Note in the DELETE
syntax, the WHERE
clause: The WHERE
clause sets out the record or records to be removed. If you skip the WHERE
clause, All records are erased.
Let's look at the "Employee" table :
ID | Name | City |
---|---|---|
1 | Suresh | Paris |
2 | Babu | London |
3 | Kumar | Paris |
Example (MySQLi Object-oriented)
In the "Employee" table, the following examples delete the record(s) for city column with the city='Paris' value :
connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Build sql delete query
$sql = "DELETE FROM Employee WHERE city='Paris'";
if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully
";
$count = $conn->affected_rows;
echo "Total deleted records : " . $count;
} else {
echo "Error: " . $sql . "
" . $conn->error;
}
// Close connection
$conn->close();
?>
Output:-
Total deleted records : 2
After executing the above program the "Employee" table look like:
ID | Name | City |
---|---|---|
1 | Suresh | Paris |
For Mysqli Procedural and PDO programs, just replace the update query with delete query and please refer the update examples in top of page.
Related Links