Query PHP and MySQL articles from the Web

Source: Internet
Author: User
Keywords Network programming Mysql tutorial
Tags access browser connect data database architecture error file function

PHP + MySQL combination is a common site to build, but how to use PHP to access the MySQL database through the Web? The following from the working principle of Web database architecture to talk about.

Query Databases from the Web: How Web Database Schemas Work

1, a user's browser makes an HTTP request to request a specific Web page, in this page departure form form submitted to the php script file (eg: results.php) to deal with;

2, Web server receives the request.php page, retrieve the file, and pass it to the PHP engine processing;

3, PHP engine began to parse the script. The main script includes the command to connect to the database and execute the query. PHP starts the connection to the MySQL server and sends the appropriate query to the server;

4, MySQL server receives the database query request, begin processing this query, and return the query results to the PHP engine;

5, PHP engine to complete the operation of the script, the HTML back to the Web server;

6, Web server HTML then returned to the client browser, the user can see the results of the response page;

Basic steps to query the database from the Web

1, check and filter the data from the user First, we will filter the user may be in the search conditions at the beginning or end of careless input blank characters, which is to use the function trim () to achieve. The reason we troubled our users with checking data was to prevent multiple interfaces from connecting to the database, as users entered from different interfaces, which could lead to security issues. Then, when preparing to use any data entered by the user, some control characters should also be properly filtered. When the user inputs data into the database, the data must be escaped. At this time, functions such as addslashes () and stripslashes And get_magic_qutoes_gpc () function. The addslashes () function adds a backslash to some characters for the purposes of database queries, etc .; stripslashes () removes the backslash character from the string; get_magic_qutoes_gpc () function adds the escape character "", The current activity configures the magic_quotes_runtime setting, returning 0 if the magic quotes are closed at runtime, 1 otherwise. We can also use htmispecialchars () to warn about the special characters in the HTML. The htmispecialchars () function converts some of the predefined characters to HTML entities. Predefined characters are: & (sum sign) becomes & "(double quotes) Become '' (single quotes) become '<(less than) become <> (greater than) Becomes>'

2, to establish a connection to the appropriate database PHP to connect to MySQL provides a library mysqli (i said improvement). When using the mysqli library in PHP it is possible to use object-oriented or procedural-oriented syntax:

A, object-oriented, @ $ db = new mysqli ('hostname', 'username', 'password', 'dbname'); returns an object

B, oriented process: @ $ db = mysqli_connect ('hostname', 'username', 'password', 'dbname'); Returns a resource that represents the connection to the database, and if you use a procedural method, you must pass this resource All other functions to mysqli. This is very similar to the handler function.

Most of mysqli's functions have an object-oriented interface and a procedural interface. The difference between the two is that the function name of the process version begins with mysqli_ and also requests the resource handle passed in by the mysqli_connect () function. For this rule, data connectable is an exception because it is created by the constructor of the mysqli object. So you need to check when trying to connect, the mysqli_connect_errno () function will return an error number in the case of a connection error, or 0 if successful.

Please note:

When connecting to a database, the usual conference error suppressor @ is the first containing code. This cleverly handle any error, you can also handle exceptions. In addition, MySQK also has some restrictions on the number of connections to the database at the same time. The MySQL_max_connections parameter determines the number of simultaneous connections. This parameter and the associated Apache parameter MaxClients is used to tell the server to reject a new connection request so that system resources are no longer being requested or used when the system is busy or when the system crashes. To set the MaxClients parameter in Apache you can edit the httpd.conf file in your system. To set the max_connections parameter for MySQLi, edit the file my.conf. Select the database to be used: Use the use dbname command on the MySQL command line; use $ db-> select_db (dbname) or mysqli_select_db (db_resource, dbname) in php.

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.