Home arrow MySQL arrow Page 4 - The Three Most Important MySQL Queries

The UPDATE MySQL Query Command - MySQL

Doing PHP queries to a MySQL database is one of the most important processes in any web application. This is where data is being fetched from or inserted into the database. If you are a beginning PHP web developer, then learning the most important MySQL queries is essential to your success in dealing with dynamic websites.

  1. The Three Most Important MySQL Queries
  2. The INSERT MySQL Query Command
  3. The SELECT MySQL Query Command
  4. The UPDATE MySQL Query Command
By: Codex-M
Rating: starstarstarstarstar / 27
July 06, 2009

print this article



One of the important MySQL queries that need to be done in PHP is UPDATE. As the command suggests, it enables you to update records in the MySQL table, without necessarily inserting a row again.

For example, you have the following data table below:

Assuming Peter Patter would like to change his password from "logan" to "weaponx," the MySQL query using UPDATE would be:


//Step 1 Connect to database

$username = "yourusername";

$password = "yoursqlpassword";

$hostname = "localhost";

$table = "usertable";

$database = "userdatabase";

$dbhandle = mysql_connect($hostname, $username, $password)

or die("Unable to connect to MySQL");

$selected = mysql_select_db($database,$dbhandle)

or die("Could not select $database");

//Initial variables

$username = 'wolverine';

$fullname = 'Peter Patter';

//Set new password for Peter Patter

$newpassword = 'weaponx';


$newpassword = mysql_real_escape_string(stripslashes($newpassword));

$username = mysql_real_escape_string(stripslashes($username));

$fullname = mysql_real_escape_string(stripslashes($fullname));

//Update records in MySQL database

mysql_query("UPDATE usertable SET password = '$newpassword' WHERE username = '$username' AND fullname = '$fullname'")

or die(mysql_error());

echo 'The database has been updated. Thank you';


The update script above will change the password of Peter Patter from "logan" to "weaponx." The script above is just a raw example; you can make the above PHP script work with HTML forms for more interactive updating of data.

>>> More MySQL Articles          >>> More By Codex-M

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Oracle Unveils MySQL 5.6
- MySQL Vulnerabilities Threaten Databases
- MySQL Cloud Options Expand with Google Cloud...
- MySQL 5.6 Prepped to Handle Demanding Web Use
- ScaleBase Service Virtualizes MySQL Databases
- Oracle Unveils MySQL Conversion Tools
- Akiban Opens Database Software for MySQL Use...
- Oracle Fixes MySQL Bug
- MySQL Databases Vulnerable to Password Hack
- MySQL: Overview of the ALTER TABLE Statement
- MySQL: How to Use the GRANT Statement
- MySQL: Creating, Listing, and Removing Datab...
- MySQL: Create, Show, and Describe Database T...
- MySQL Data and Table Types
- McAfee Releases Audit Plugin for MySQL Users

Developer Shed Affiliates


Dev Shed Tutorial Topics: