Introduction to the Treasure Chest PHP Web Query Database basic Steps _php Tutorial

Source: Internet
Author: User
Tags control characters mysql command line
PHP is to develop web dynamic page of the preferred programming, recently read a book harvest a lot, now and everyone to share the PHP Web query database knowledge, let's come together to see it. Basic steps for querying a database from the PHP Web:

1. Check and filter data from the user first, we will filter white space characters that the user may accidentally enter at the beginning or end of their search criteria, which is implemented with function trim (). We're so in trouble. The reason for 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 issues.

Then, when you are ready to use any data entered by the user, you also need to filter some control characters appropriately, and when the user enters the data into the database, the data must be escaped, at which point the stolen function has the addslashes () function, the stripslashes () function, and the Get_magic_qutoes The _GPC () function. The Addslashes () function adds a backslash to some characters, such as a database query statement, and the Stripslashes () function removes the backslash character from the string; the GET_MAGIC_QUTOES_GPC () function Magic adds the escape character "", Gets the current active configuration Magic_quotes_runtime setting, returns 0 if the runtime closes the magic quotation mark, or returns 1. We can also use Htmispecialchars () to alert encoding of special meaning characters in HTML, Htmispecialchars () function to convert some predefined characters to HTML entities predefined characters are:& (and number) to become & " (double quotes) becomes "' (single quote) becomes ' < (less than) becomes < > (greater than) becomes >

2. Establish a connection to the appropriate database PHP provides a library of functions for connecting MySQL mysqli (i for 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 that represents the connection to the database, and if a process method is used, This resource must be passed to all other functions of mysqli.

Most functions that are very similar to the MYSQLI function have object-oriented interfaces and procedural interfaces, and the difference is that the function name of the procedure version begins with Mysqli_ and requires the resource handle that is obtained by the incoming Mysqli_connect () function. For this rule, data can be concatenated as an exception because it is created by the constructor of the Mysqli object. Therefore, when attempting to connect, a check is required, and the Mysqli_connect_errno () function returns an error number when a connection error occurs and returns 0 if successful.

Note: When connecting to a database, it is usually the meeting error suppressor @ as the first containing code. This makes it possible to handle any error skillfully, or through exceptions. In addition, MYSQK has a certain limit on the number of connections to the database at the same time. The mysqli parameter max_connections determines the number of simultaneous connections, and the role of this parameter and the associated Apache parameter maxclients is to tell the server to reject new connection requests, thus ensuring that system resources are not requested or used when the system is busy or when the system is 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.

Select the database used: Use dbname on the MySQL command line, command, $db->select_db (dbname) in PHP, 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 Returns a result object, and the procedure version returns a result resource. Regardless of the method, the results are saved in the $result variable for later use. Returns False if the function fails to run.

4. Get query results use different functions to take query results out of a result object or identifier in different ways, and the resulting object or identifier is the key to accessing the query's returned rows.

Usually we want to get the number of rows in the result set and use the MYSQLI_FETCH_ASSOC () function. Number of rows returned: $num _results = $result->num_rows; (the number of rows is saved in the Num_rows member variable of the object) or $num_results = Mysqli_num_rows ($result); It then iterates through each row, calls $row = $result->fectch_assoc () in the loop, or $row = Mysqli_fetch_assoc ($result), and returns information about the row. If the object returns a row, each keyword is a property name, each value is the corresponding value in the property, and an array is returned if returned as a resource.

There are other ways to get results from the result identifier, such as using $row = $result->fecth_row ($result), or $row = Mysqli_fetch_row ($result), and retrieving the results into an array of enumerations ; You can also use $row = $result->fecth_object (), or $row = Mysqli_fecth_object ($result), and the river goes back to an object.

5. Release the result set from the database disconnect: $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 because they will be automatically closed when the scripts are executed.

The above is the PHP Web query database Basic steps, do not know everyone learned it? Try it quickly.


http://www.bkjia.com/PHPjc/446449.html www.bkjia.com true http://www.bkjia.com/PHPjc/446449.html techarticle PHP is to develop web dynamic page of the preferred programming, recently read a book harvest a lot, now and everyone to share the PHP Web query database knowledge, let's come together to see it. ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.