PHP is the preferred programming method for developing dynamic WEB pages. I learned a lot from reading a book recently. Now I would like to share with you the knowledge of PHP Web query database. Let’s take a look together. Basic steps to query a database from PHP Web:
1. Check and filter data from users First, we will filter for whitespace characters that users may have accidentally entered at the beginning or end of their search criteria, which is Use the function trim() to achieve this. The reason why we go to such trouble to check user input data is to prevent multiple interfaces from connecting to the database, because users enter from different interfaces, which may cause security issues.
Then, when preparing to use any data input by the user, some control characters must also be appropriately filtered. When the user inputs data into the database, the data must be escaped. At this time, the stolen functions include addslashes() function, stripslashes() function and get_magic_qutoes_gpc() function. The addslashes() function adds a backslash before certain characters for database query statements, etc.; the stripslashes() function removes the backslash characters in the string; the get_magic_qutoes_gpc() function magically adds the escape character "" to get The currently active configuration magic_quotes_runtime setting, returns 0 if magic quotes are turned off at runtime, 1 otherwise. We can also use htmispecialchars() to encode special meaning characters in HTML. The htmispecialchars() function converts some predefined characters into HTML entities. The predefined characters are: & (ampersand) becomes & " (double quotation mark) becomes " ' (single quote) becomes ' < (less than) becomes < > (greater than) becomes >
2. Establish a connection to the appropriate database. PHP provides the function library mysqli (i represents an improvement).
When using the mysqli function library in PHP, you can use object-oriented or process-oriented syntax:
1) Object-oriented, @ $db = new mysqli('hostname','username' ,'password','dbname'); returns an object
2) Process-oriented: @ $db = mysqli_connect('hostname','username','password','dbname'); returns a resource , this resource represents the database connection, and if the procedural method is used, this resource must be passed to all other functions of mysqli.
This is very similar to the processing function. Most functions of mysqli have object-oriented interfaces and procedural interfaces. The difference between the two is that the function name of the procedural version starts with mysqli_ and requires the mysqli_connect() function to be passed in. The resource handle obtained. Data joinability is an exception to this rule because it is created by the mysqli object's constructor. Therefore, you need to check when trying to connect. The mysqli_connect_errno() function will return an error number when a connection error occurs. If successful, it will return 0.
Please note: When connecting to the database, usually the error suppressor is used @ as the first containing code. This allows any errors to be handled gracefully or through exceptions. In addition, MySQK has certain limits on the number of connections to the database at the same time. The MySQLi parameter max_connections determines the number of simultaneous connections. The function of this parameter and the related Apache parameter MaxClients is to tell the server to reject new connection requests, thereby ensuring that system resources will not be requested or used when the system is busy or when the system is paralyzed. To set the MaxClients parameters in Apache, you can edit the httpd.conf file in the system. To set the max_connections parameter for MySQLi, edit the file my.conf.
Select the database to use: Use the use dbname; command on the MySQL command line; in PHP, you can use $db->select_db(dbname); or mysqli_select_db(db_resource,dbname).
3. Query the database To execute a database query, you should first construct a query statement: $query = "select from user"; and then run $result = $db->query($query); or $result = mysqli_query($db,$query); The object-oriented version will return a result object; the procedural version will return a result resource. Regardless of the method, the result is saved in the $result variable for later use. If the function fails, it will return false.
4. Get the query results. Use different functions in different ways to get the query results out of the result object or identifier. The result object or identifier is the key to accessing the rows returned by the query.
There are other ways to get the result from the result identifier, for example: use $row = $result->fecth_row($result); or $row = mysqli_fetch_row($result); to get the result back to an enumeration array; you can also use $row = $result->fecth_object(); or $row = mysqli_fecth_object($result); to return to an object line by line.
5. Disconnect from the database first release the result set: $result->free(); or mysqli_free_result($result); and then close the database connection: $db->close() or mysqli_close($db); Strictly speaking, this is not necessary as they will be automatically closed when the script is finished executing.
The above are the basic steps for PHP Web query database. I wonder if you have learned it? Try it now.