PHP MySQL Update

Posted by tutor | Posted in PHP Tutorial | Posted on 18-12-2009-05-2008

0

The UPDATE statement is used to update existing records of a table in a database.

Syntax

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

WHERE clause is included in the update statement to conditionally update the records. It specifies which record or records should be updated. If there is no WHERE clause, all records will be updated.

    For eg:

    Zimbabwe Harare
    Poland Warsaw

The following example updates some data in the “Country” table:

    <?php
    $con = mysql_connect(”localhost”,”tutor”,”tutor123″);
    if (!$conn)
    {
    die(’Could not connect: ‘ . mysql_error());
    }
    mysql_select_db(”my_db”, $con);
    mysql_query(”UPDATE Country SET Capital = ‘Poland’
    WHERE FirstName = ‘Poland’”);
    mysql_close($conn);
    ?>

After the update, the “Country” table will look like this:

    Country Capital
    Zimbabwe Harare
    Poland Poland

Write a comment

Twitter Users
Enter your personal information in the form or sign in with your Twitter account by clicking the button below.