Step by step, we will teach you how to use PHP + MySql to set up website No. 1 homepage & amp; database connection, mysqlno.1

Source: Internet
Author: User

Step by step, we will teach you how to use PHP + MySql to set up website No. 1 homepage and database connection, mysqlno.1

In this section, we will look at the main interface after the user enters the webpage.

In general, the default homepage is index point xxx, suchIndex. php, index.html, index. jspAnd so on. Let's take a look at our index. php.

Index. php


Reminder:

In <? Php?> ,Do not contain any spaces orOther characters, that is, do not <? Add a space character in front of php, then?> Do not include other characters. Otherwise, the following message may appear:


I have seen such a situation before. After google, it means that headers must be executed at the beginning, and no output is available before header.

Once again,StackoverflowIt is really powerful. Basically, all the questions can be answered above.


Next, let's look at twoInclude_once ()It is equivalent to the file imported in import. In the entire index. php, we moved three include files, which areDb_fns.php, header. php and footer. php.

Let's take a look.

Db_fns.php


At this stage, we only need to use thisDb_connect ()The function is fine. Do not forgetEnd with?>

This function is used to connect to the database. The three parameters in the mysql_connect () function areDatabase address, database user name, and password.

Reminder: you can download the source code from the previous blog. I changed the password to 'Password'. The actual password is different.

Then useMysql_select_db ()Function to select our database.

In this way, you can connect to the database we have previously created.

Header. php and footer. php is actually equivalent to an independent framework. In different pages, we only need to modify the content, and the peripheral framework remains unchanged, so we can extract them separately.

Header. php


We can see that there is no php code in header. php, And it is pure HTML code. I will not explain it more here. They are all very simple tags, link addresses, CSS, etc.

Footer. php


Similarly, footer is a pure HTML Tag. In combination, the two parts are a complete framework, and what we need to do is to modify the middle part so that they can display different content on different pages.

Let's focus on the php code block section in the middle of index. php. As we move slowly, it will always be a little difficult at the beginning:

I will post the code again:


In line 1 of the code, we wrote the first SQL statement in our project. We filtered all the content from the page table, if it is like this in the database:


We can see that the page table contains all types of articles, which are represented by code. descriptions of each type are also stored accordingly.

Then we useMysql_queryThe function saves the select result toVariable pages_resultInside:

$pages_result = mysql_query($pages_sql);

Then, check the 17th lines of code in WhileStatement, we use a function Mysql_fetch_assocTo store the search results in a dictionary.

If you google this function, you will find that there are three functions in mysql:

Mysql_fetch_row, mysql_fetch_array, and mysql_fetch_assoc

Row can only be used with subscripts 0, 1, and 2 to select the content of different columns. assoc can be selected using the database column name, array can be used with numbers or keys.

Therefore, when the while statement is executed for the first time, the variable pages is actually a key-value pair of key and value. The key is code and description, values are news and 'The top news stories from around The world ';

So we can understand how the select statements of rows 18 and 19 are made up? Leave a message if you don't understand it. Let's study together!

Through the while statement loop, we get three select statements in sequence, and select all the stories sorted by page from the stories table.



In this way, we can display the first story of different pages (because if is not while, only the content of the first story is displayed );

Then we useResize_image.phpThe corresponding image is displayed. Here we will introduce it in the next chapter, including some links in the database's small tips and header, which we will introduce later.


In this way, write down the code and enter our website in the browser to display the corresponding content. Of course, if you click the above link, an error will be prompted. Let's improve it.


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.