Use PHP to access MySQL (1) _ PHP Tutorial

Source: Internet
Author: User
Use PHP to access MySQL (1 ). In this chapter, we will learn how to store and display information to the database on a Web page. We have already installed the MySQL relational database engine and the PHP Server. in this chapter, we will learn how to store and display information to the database on a Web page. We have installed the MySQL relational database engine and the PHP script language on the server, and learned the basic knowledge about them. After learning this chapter, we will understand how to use these two new tools to build a database-driven website!

Review

Before we proceed, let's look back at the purpose of our learning as valuable. Now we have two powerful new tools in our system: script language PHP and database engine MySQL. It is important to figure out that the two are working collaboratively.

The essence of a database-driven website is to allow the content of the website to exist in a database, in addition, we can use this database to dynamically generate Web pages so that our visitors can display them through a standard Web browser. So at the end of your system is a visitor who accesses your site. he accesses HTTP: // WWW. YOURSITE. COM to obtain a standard HTML Web page and display it in a Web browser. On the other end of your system, one or more data tables are stored in the content of your site in a MySQL database that only understands how to respond to SQL queries (commands.

The PHP scripting language assumes the role of liaison officer between the two. using PHP, you can compile a standard HTML "template ", this "template" determines the appearance of your site (including drawing and page design ). At this time, the content belongs to this "template". you can use some PHP code to connect to the MySQL database and use SQL query to obtain data and display it in the corresponding position, the SQL query here is the same as that we used to create a joke data table in chapter 2.

Now you should have a clear understanding of what will happen when visitors access a page of your database-driven website:

The visitor's Web browser uses a standard URL to request this page.

The Web server software (Apache, IIS, or others) determines that the requested page is a PHP script, so it is interpreted using its PHP plug-in before responding to this page request.

Some PHP commands (which we haven't learned yet) will connect to the MySQL database and request the content on this web page from the database.

The MySQL database responds and sends the requested content to the PHP script.

The PHP script stores the content in one or several PHP variables and outputs it as part of the web page using the echo function we are familiar.

The PHP plug-in completes processing and returns the generated HTML copy to the Web server.

The Web server sends this HTML copy to the Web browser. this is a standard HTML file, but it is not directly from an HTML file, but from the output provided by the PHP plug-in.

Connect to MySQL using PHP

Before getting the content contained on our Web page from our MySQL database, we must first know how to establish a connection with MySQL. In chapter 2, we use a program named mysql for such a connection. PHP does not need such a program. its support for connecting to MySQL is language-built. The following function is used to establish such a connection:

Mysql_connect (  

Bytes. We have installed the MySQL relational database engine and the PHP server before...

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.