This a small tutorial on how to update rows in a MySQL database using prepared statements. In this example, I will be using PHP’s PDO object.
For example, let us say that we have a table called cars and that we want to update the row with the ID “90” (it’s Primary Key). In this particular code example, we will be updating the “model” column to “Civic”.
A drill-down of the code above:
- We connect to MySQL using the PDO extension.
- We create an UPDATE SQL statement using placeholders. In this case, we used the placeholders :model and :id.
- We prepare our UPDATE statement.
- We set $id to 90 and $model to ‘Civic’.
- We bind our variables to the placeholders that we created in Step 2.
- We execute the prepared statement. It is at this stage that the row is updated.
0 comments:
Post a Comment