Update updates
the update SET syntax is used to modify data in the Update data table.
Grammar:
UPDATE tb_name SET column1 = New_value1,column2 = New_value2,... WHERE definition
This syntax updates the value of Column1 in a record in the data table that matches the WHERE condition to the value of New_value1,column2 to New_value2, and so on. If you omit the WHERE condition, the column values for all records in the table are updated.
Example:
<?php
$conn = @mysql_connect ("localhost", "root", "root123");
if (! $conn) {
die ("Connection Database failed:". Mysql_error ());
}
mysql_select_db ("Test", $conn);
mysql_query ("Set names ' GBK '");
$sql = "UPDATE user SET email = ' xiaoming@163.com ' WHERE username = ' Xiaoming '";
if (mysql_query ($sql, $conn)) {
echo updates the data successfully! ";
} else {
echo failed to update data:. Mysql_error ();
>
Pre-Update data:
The example modifies the email of username in the user table for Xiaoming to xiaoming@163.com.
Post-Update data:
An UPDATE expression
The UPDATE syntax allows the SET to be followed by an expression.
Example 1:
UPDATE article SET PV = pv+1 WHERE id = 123
This example lets an article with ID 123 click on the reading and add 1 clicks.
Example 2:
UPDATE persondata SET age = age*2, age = age+1
The example SET is followed by two expressions: Age = age*2 (ages doubled), aged = age+1 (plus 1). The case of this multiple expression is performed in Left-to-right order.
Delete from deletes data
Delete Deletion
The delete from syntax is used to delete data records from a datasheet.
Grammar:
DELETE from Tb_name WHERE definition
This syntax deletes data records that match the WHERE condition in the datasheet. If you omit the where condition, all the records in the table are deleted.
Example:
<?php
$conn = @mysql_connect ("localhost", "root", "root123");
if (! $conn) {
die ("Connection Database failed:". Mysql_error ());
}
mysql_select_db ("Test", $conn);
mysql_query ("Set names ' GBK '");
$sql = "DELETE from user WHERE username = ' Xiao Wang '";
if (mysql_query ($sql, $conn)) {
echo "delete". Mysql_affected_rows (). "Data record." ";
} else {
exit ("Delete data failed:". Mysql_error ());
}
? >
Delete data successfully, browser output:
Deletes 1 data records.
Before deleting data:
Post-delete data:
If no qualifying records are deleted, mysql_query () still returns TRUE (unless SQL syntax errors). Therefore, to determine exactly whether a data record is deleted, you need to call the Mysql_affected_rows () function, which returns the number of rows that were affected by the most recent insert,update or DELETE query.
Tips
If you just want to delete a field data for a record, use the UPDATE SET syntax to empty it.