In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces you what is the basic steps of PHP Web query database, the content is very detailed, interested friends can refer to, hope to be helpful to you.
PHP is the development of WEB dynamic page programming, recently read a book to gain a lot, now share with you the knowledge of PHP Web query database, let's take a look at it. The basic steps for querying a database from PHP Web:
1. Check and filter the data from the user first, we will filter the white space characters that the user may accidentally enter at the beginning or end of their search condition, which is achieved with the function trim (). The reason we have so much trouble checking user input data is to prevent multiple interfaces from connecting to the database, because users enter from different interfaces, which can lead to security problems.
Then, when preparing to use any data entered by the user, some control characters should be filtered appropriately. When the user enters the data into the database, the data must be escaped. The stolen functions are addslashes () function, stripslashes () function and get_magic_qutoes_gpc () function. Addslashes () function adds a backslash before some characters for database query statements, etc.; stripslashes () function removes the backslash character from the string; get_magic_qutoes_gpc () function magically adds the escape character "" to get the current active configuration magic_quotes_runtime setting. If the magic quotation mark is turned off at run time, 0 is returned, otherwise 1 is returned. We can also use htmispecialchars () to alert the special meaning characters in HTML. The htmispecialchars () function converts some predefined characters into HTML entities. The predefined characters are: & (and signs) become & "(double quotes) become" (single quotes) become'
< (小于) 成为 < >(greater than) become >
two。 Establishing a connection to the appropriate database PHP provides a library mysqli (I for improvement) for connecting to MySQL.
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) procedure oriented: @ $db = mysqli_connect ('hostname','username','password','dbname'); returns a resource that represents the connection to the database, and if you use the procedure method, you must pass this resource to all other functions of mysqli.
This is very similar to handling functions. Most functions of mysqli have object-oriented interfaces and procedural interfaces. The difference between the two is that the name of the function in the procedure version begins with mysqli_ and requires that the resource handle obtained by the mysqli_connect () function be passed. For this rule, data connectability is an exception because it is created by the constructor of the mysqli object. So you need to check when you try to connect, and the mysqli_connect_errno () function returns an error number when a connection error occurs, or 0. 0 if successful.
Please note that when connecting to a database, the meeting error suppressor @ usually contains code as *. This can cleverly handle any error, or you can handle it through exceptions. In addition, MySQK has a limit 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, thus ensuring that system resources will not be requested or used when the system is busy or paralyzed. To set the MaxClients parameter in Apache, you can edit the httpd.conf file in the system. To set the max_connections parameter for MySQLi, you can edit the file my.conf.
Choose 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. To query a database, you should first construct a query statement: $query = "select from user"; then run $result = $db- > query ($query); or $result = mysqli_query ($db,$query); the object-oriented version will return a result object; the process version will return a result resource. Either way, the result is saved in the $result variable for later use. If the function fails, it returns false.
4. To obtain the query results, different functions are used to extract the query results from the result object or identifier in different ways, which is the key to accessing the returned rows of the query.
Usually we get the number of rows recorded in the result set and use the mysqli_fetch_assoc () function. Return the number of rows: $num_results = $result- > num_rows; (the number of rows is saved in the object's num_rows member variable) or $num_results = mysqli_num_rows ($result); then use a loop to iterate through each row, calling $row = $result- > fectch_assoc () in the loop; or $row = mysqli_fetch_assoc ($result); returns the line's information. If an object returns a row, each keyword is a property name, and each value is the corresponding value in the attribute; if returned as a resource, an array is returned.
There are other ways to get the result from the result identifier, such as using $row = $result- > fecth_row ($result); or $row = mysqli_fetch_row ($result); fetching the result back into an enumerated array; or using $row = $result- > fecth_object (); or $row = mysqli_fecth_object ($result); Jiang goes back to an object.
5. Disconnect from the database first release the result set: $result- > free (); or mysqli_free_result ($result); then close the database connection: $db- > close () or mysqli_close ($db); strictly speaking, this is not necessary, because they will be automatically closed when the script is finished.
On the PHP Web query database what is the basic steps to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.