| Start | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Close connection When we are finished querying the database, it is recommended to close the connection to free up resources. Object oriented style Procedural style Note: The connection to the server will be closed as soon as the execution of the script ends, unless it's closed earlier by explicitly calling the close connection function. Use Case: If our script has a fair amount of processing to perform after fetching the result and has retrieved the full result set, we definitely should close the connection. If we were not to, there's a chance the MySQL server will reach its connection limit when the web server is under heavy use. |
||||||||||
| MySQLi connect Object oriented style Connect to Server Set the default database: Connect to Database Procedural style Connect to Server Set the default database: Connect to Database Verify Database Connection Object oriented style Procedural style |
||||||||||
| Loop through MySQLi results PHP makes it easy to get data from your results and loop over it using a
Object oriented style Procedural style To get exact information from results, we can use: |
||||||||||
| Prepared statements in MySQLi Please read Preventing SQL injection with Parametrized Queries (opens new window) for a complete discussion of why prepared statements help you secure your SQL statements from SQL Injection attacks The For both examples, we assume that The Object oriented style Procedural style The first parameter of
Your list of parameters needs to be in the order provided in your query.
In this example For retrieving data, see How to get data from a prepared statement (opens new window) |
||||||||||
| Escaping Strings Escaping strings is an older (and less secure) method of securing data for insertion into a query. It works by using MySQL's function mysql_real_escape_string() (opens new window) to process and sanitize the data (in other words, PHP is not doing the escaping). The MySQLi API provides direct access to this function At this point, you have a string that MySQL considers to be safe for use in a direct query So why is this not as secure as prepared statements (opens new window)? There are ways to trick MySQL to produce a string it considers safe. Consider the following example
|
||||||||||
| MySQLi query The Object oriented style Procedural style CAUTION A common problem here is that people will simply execute the query and expect it to work (i.e. return a mysqli_stmt object (opens new window)). Since this function takes only a string, you're building the query first yourself. If there are any mistakes in the SQL at all, the MySQL compiler will fail, at which point this function will return The above code will generate a PHP Fatal error: Call to a member function fetch_assoc() on a non-object The procedural error is similar, but not fatal, because we're just violating the expectations of the function. You will get the following message from PHP mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given You can avoid this by doing a test first |
||||||||||
| Debugging SQL in MySQLi So your query has failed (see MySQLi connect (opens new window) for how we made How do we find out what happened? or procedural You should get an error similar to Table 'my_db.non_existent_table' doesn't exist |
||||||||||
How to get data from a prepared statement Prepared statementsSee Prepared statements in MySQLi (opens new window) for how to prepare and execute a query. Binding of resultsObject-oriented style Procedural style The problem with using In both cases, we're assigning the We can then loop as follows: Object-oriented style Procedural style The drawback to this is that you have to assign a lot of variables at once. This makes keeping track of large queries difficult. If you have MySQL Native Driver ( Object-oriented style Procedural style This is much easier to work with because now we're getting a mysqli_result (opens new window) object. This is the same object that mysqli_query returns (opens new window). This means you can use a regular result loop (opens new window) to get your data. What if I cannot install |
||||||||||
| MySQLi Insert ID Retrieve the last ID generated by an Object-oriented Style Procedural Style Returns zero if there was no previous query on the connection or if the query did not update an AUTO_INCREMENT value. Insert id when updating rows Normally an Setup for examples to follow: The case of IODKU performing an "update" and The case where IODKU performs an "insert" and Resulting table contents: RemarksFeaturesThe mysqli interface has a number of benefits, the key enhancements over the mysql extension being:
It features a dual interface (opens new window): the older, procedural style and a new, object-oriented programming (OOP) (opens new window) style. The deprecated AlternativesAn alternative to the |
Read Article
https://develop.horje.com/learn/1434/reference