Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

PHP MySQL Update


May 11, 2021 PHP


Table of contents


PHP MySQL Update

You can update the data in the MySQL database as needed!

The UPDATE statement is used to modify the data in the database table.


Update the data in the database

The UPDATE statement is used to update records that already exist in the database table.

Grammar

UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value


Note: Note the WHERE clause in the UPDATE syntax. T he WHERE clause specifies which records need to be updated. If you want to save the WHERE clause, all records will be updated!

To learn more about SQL, visit our SQL tutorial.

In order for PHP to execute the statement above, we must use mysqli_query () function. This function is used to send queries or commands to MySQL connections.

Instance

In the previous sections of this tutorial, we created a table called "Peoples" that looks like this:

FirstName LastName Age
Peter Griffin 35
Glenn Quagmire 33

The following example updates some of the data from the "Peoples" table:

<?php
 $con=mysqli_connect("example.com","peter","abc123","my_db");
 // Check connection
 if (mysqli_connect_errno())
 {
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }

 mysqli_query($con,"UPDATE Persons SET Age=36
 WHERE FirstName='Peter' AND LastName='Griffin'");

 mysqli_close($con);
 ?>

After this update, the Persons table looks like this:

FirstName LastName Age
Peter Griffin 36
Glenn Quagmire 33