Wednesday, April 23, 2008

MySQL Update Example using PHP

Once again we will be working with the data from a previous example. Sandy has just had a birthday and she now 22 years old. Our job now is to update her age using MySQL commands like UPDATE, SET, and WHERE.

  • UPDATE - Performs an update MySQL query
  • SET - The new values to be placed into the table follow SET
  • WHERE - Limits which rows are affected

PHP & MySQL Code:

// Connect to MySQL

// Get Sandy's record from the "example" table
$result = mysql_query("UPDATE example SET age='22' WHERE age='21'")
or die(mysql_error());


$result = mysql_query("SELECT * FROM example WHERE age='22'")
or die(mysql_error());

// get the first (and hopefully only) entry from the result
$row = mysql_fetch_array( $result );
echo $row['name']." - ".$row['age']. "
";
?>

No comments: