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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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:

<?php

//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';

//sanitize

$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
   

MYSQL ARTICLES

- Xeround Releases Free Version of MySQL Cloud...
- Oracle Announces New MySQL Specialization
- Constant Contact Chooses SkySQL for MySQL Su...
- Revoke Statement in MySQL
- The Grant Statement in MySQL
- SuccessBricks Announces ClearDB Availability...
- Building a PHP ORM: Deploying a Blog
- TROSYS Launches Free MySQL Manager and Admin...
- Building an ORM in PHP: Domain Modeling
- Building an ORM in PHP
- MySQL Leads Open Source Market, Gets Cluster...
- Oracle Announces Milestone Release for MySQL
- How to Stop SQL Injection Attacks
- New Defragmentation Solution for SQL Server
- Comparison of MyISAM and InnoDB MySQL Databa...


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap

Dev Shed Tutorial Topics: