php-mysqlHow to update to null value in MySQL using PHP?
Updating a null value in MySQL using PHP can be done using the UPDATE
statement. The following example code will update the name
column of the users
table to NULL
if the id
is 1
:
$sql = "UPDATE users SET name = NULL WHERE id = 1";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
Output example
Record updated successfully
Code explanation
$sql
: This is the SQL statement that will be used to update thename
column of theusers
table toNULL
if theid
is1
.$conn->query($sql)
: This is the function that will execute the SQL statement.echo "Record updated successfully"
: This is the output that will be displayed if the record is updated successfully.echo "Error updating record: " . $conn->error
: This is the output that will be displayed if there is an error updating the record.
Helpful links
More of Php Mysql
- How to get the error message for a MySQL query using PHP?
- How to list databases in PHP and MySQL?
- How to insert an array into a MySQL database using PHP?
- How to use a variable in a MySQL query using PHP?
- How to get the version of MySQL using PHP?
- How to use utf8mb4_unicode_ci in MySQL with PHP?
- How to get the first row of a result in MySQL using PHP?
- How to order by a column in ascending order in MySQL using PHP?
- How to join tables with PHP and MySQL?
- How to escape JSON in PHP and MySQL?
See more codes...