Getting Started with PHP and MySQL (8)

Source: Internet
Author: User
Tags getting started with php php server
In this chapter we will learn how to store information in a Web page and display it in a database. We have previously installed MySQL, the relational database engine, and the PHP server-side scripting language, and learned the basics about them. After completing this chapter, we will understand how to use these two new tools to build a database-driven website!

Review of the previous section

Before we go on, it is worthwhile to look back at the purpose of our study. Now there are two powerful new tools in our system: scripting language PHP and database engine MySQL. Figuring out the two is important if you work together.

The essence of a database-driven Web site is to allow the content of the site to exist in a database, and it can be used to dynamically generate Web pages to allow our visitors to display it through a standard Web browser. So at one end of your system is a visitor to your site, who accesses HTTP://WWW.YOURSITE.COM to get a standard HTML-formatted Web page and display it in a Web browser. The other end of your system is stored in one or several data tables in a MySQL database that only understands how to respond to SQL queries (commands) in the content of your site.

The PHP scripting language assumes the role of a liaison between the two, using PHP, you can write a standard HTML "template" that determines the appearance of your site (including drawing and page design). At this point the content belongs to this "template", you can use some PHP code to connect to the MySQL database and use SQL query to get the data and display it in its corresponding location, where the SQL query is the same as we used in the second chapter to build a joke data table.

Now you should have a clear understanding of what happens when a visitor accesses a page of your database-driven site:

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 and therefore interprets it with its PHP plugin before responding to this page request.

Some PHP commands (which we haven't learned) connect to the MySQL database and request content from the Web page to 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 uses our familiar echo function to output it as part of the Web page.

The PHP plugin finishes processing and returns the resulting HTML copy to the Web server.

The Web server sends this HTML copy to the Web browser, which will be a standard HTML file, except that it is not directly from an HTML file, but from the output provided by the PHP plugin.

Connect MySQL with PHP

Before we get the content from our web page from our MySQL database, we first have to know how to establish a connection to MySQL. In the second chapter, we use a program called MySQL to do such a connection. PHP does not require such a program, the support for connecting to MySQL is language built-in. The following function is used to establish such a connection:

Mysql_connect (, , );

Here, the IP address or hostname of the computer on which the MySQL service software is running (if this is the same as the computer running the Web services software, you can use "localhost"), and is the user name you used to connect to the MySQL server in chapter two and password.

You may remember that functions in PHP tend to return (output) a value when called. Please don't worry we don't remind you that we will detail it for you when we first touch a function. Most functions, when called, return a value that can be stored in a variable for the next use. For example, the Mysql_connect function we described above will return a number to identify the connection that has been established. Because we are going to use this connection, we have to save this value. Here is an example of how to connect to our MySQL database:

$DBCNX = mysql_connect ("localhost", "root", "mypasswd");

It should be explained that for your MySQL server, the values of the three parameters in this function may be different. You should have noticed that here our Mysql_connect returns a value (which we call a connection identifier), which we store in the variable $DBCNX.

  • 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.