PHP: Update data in a MySQL Database | |||||
|---|---|---|---|---|---|
| Adam | 06:28 pm - 07/06/2010
| ||||
| PHP: Update data in a MySQL DatabasePHP can easily update data into a MySQL database, once you have connected to your database you can now update any data within it.Syntax UPDATE table_name SET column1 = 'value1', column2 = 'value2' WHERE column3 = 'value3' Example $query = "UPDATE users SET name = 'Adam', surname = 'Tester' WHERE id = '3'"; $result = mysql_query($query); Explanation The first line is a variable called $query, this variable contains the SQL query that will be used to update data in the database. UPDATE users This means that the query will update data in the table named 'users'. SET name = 'Adam', surname = 'Tester' This is telling MySQL to replace whatever is in column 'name' to 'Adam'. WHERE id = '3' This is telling MySQL where to update the data within the table, in this case it will update any row with the ?id? of ?3?. mysql_query($query) This part is actually making the query. It uses the function mysql_query with the variable $query. Checking if it worked Its good practise to always check if a query worked or not, so here is how to do it. You may have noticed that mysql_query($query)was stored in the variable $result. This means that the result of the function will be stored in the variable named $result (I should have chosen a different name to make it less confusing). This means you can check if it worked by using a simple if statement. $query = "UPDATE users SET name = 'Adam', surname = 'Tester' WHERE id = '3'"; $result = mysql_query($query); if($result){ echo 'It worked!'; }else{ echo 'It didn?t work!' } ?> For this example if it was successful you would see It worked! printed out on the page, but if it failed for some reason then it would print It didn't work!. | ||||
| There are no replys to show. |
|---|
Page generated in 0.1025, using 3.29MB









