A connection to a MySQL database is persistent from the time the connection is opened to the time the connection is explicitly closed of the script terminates.
A typical method to connect with a database is as follows:
In this code, the following happens:
After a connection is made, queries can be formulated and executed.
An SQL query is generally speaking a string. The main types are as follows:
Note that SELECT is a non-DML (data manipulating language) query, while INSERT, DELETE and UPDATE are DML queries. As a non-DML query, a SELECT query is often followed by another function call to get the result of the query.
Queries are formulated much like SQL queries used in the interactive client “mysql”. However, when a query is formulated in PHP, it is possible to specify components of a query using variables.
For example, if the PHP variable “employeeId” stores a single employee ID that is used look up the row in an employee table, then the following code can be used to make a query:
This is fine as long as the variable employeeId is properly validated so that the query cannot be used to perform unwanted operations. For example, if the variable employeeId contains a single quote, then it can easily extend the intended query.
A safe method to make sure any variable to be expanded in a query is safe is to use the function mysqli_real_escape_string. This function does not actually validate a string, but rather it adds the necessarily escaping needed so that a string can be safely used in single quoted values passed to an SQL query. In our example, the new code may look like the following (assuming the value in variable employeeId is not previously validated:
A select query can be quite complex. However, the most general use consists of a list of fields, a table where rows are retrieved from, a filter to limit results to rows that satisfy the filter criterion, and a order specification so that rows are returned in a particular order.
For example, observe the following query:
In this example, the fields to be returned include the ID and the start date. Rows are retrieved from the employee table. Only rows with IDs between 3000 and 4000 exclusively are included in the result. Last, the rows are retrieved in a descending order based on the last name of the rows. Note that the last name is not even a part of the fields returned!
Of course, SELECT statements an be simpler or more complex than the one shown here. For example, JOIN operations can significantly increase the complexity of a SELECT query.
The following is a general framework of using a SELECT query in PHP code:
An insert query is used to insert one or more rows in a table. There are several formats. To insert just one row, the following code structure is suitable:
In this example, four fields are specified in the INSERT query. Note that the number of fields specified depends on how the table is set up. If a field is optional, it does not need a value in an INSERT statement. Likewise, if a field is automatically assigned, it also does not (in fact, should not) have a value in an INSERT statement.
A delete query is used to remove one or more rows from a table. The following is a general structure of PHP code to execute a DELETE query:
Note that the WHERE statement can be complex using logical operators. Be careful when you use a DELETE query because it can potentially delete many rows from a table.
An update query is used to change/alter one or more rows in a table. An UPDATE query consists of a section to specify updates, and a section to filter which rows in a table is affected.
The following is an example of how to use UPDATE in a PHP script:
Note that the WHERE section can be much more complex. Furthermore, the SET section can also use fields of the same row as the right hand side of the equal.