Step by step to teach you to build your site with Php+mysql Home & database connections

Source: Internet
Author: User

In this section we look at the main interface after the user enters the page.

In general, the default home page is index point xxx, such as index.php, index.html, index.jsp , and so on. Let's take a look at our index.php.

index.php


Remind:

At both ends of <?php?>, do not appear any spaces or other characters, that is, do not precede the <?php with spaces such as characters, and then?> do not appear after the rest of the characters, otherwise you may be prompted:


I have been in this situation before, Google said headers must be placed at the beginning of the execution, can not first have output and then the header.

Again,StackOverflow really is too strong, basically all the questions can be found on the answer.


Next, we look at two include_once () equivalent to import incoming files, throughout the index.php we moved include 3 files, respectively, db_fns.php, header.php and footer.php.

Let's look at each other.

db_fns.php


At this stage we just need to use this db_connect () function, and don't forget to add?> at the end.

This function is used to connect to the database, the mysql_connect () function inside the 3 parameters, respectively, the address of the database, the user name of the database, the user's password .

Friendship reminds, the previous blog can be downloaded to the source code, the password I was changed to the ' password ', the real password is not the same OH.

Then use the mysql_select_db () function to select our database.

This will allow us to connect to the database we built earlier.

Header.php and footer.php are actually equivalent to the framework of the Independent, in different pages, we only need to modify the contents of the inside, and the periphery of this frame is constant, so we picked them out alone.

header.php


Can see, header.php inside actually no PHP code, are pure HTML code, here do not do more explanation, are very simple tags, link address, CSS and so on

footer.php


Footer, too, are plain HTML tags. The two parts together look like a complete framework, and then what we need to do is modify the middle part so that they show different content on different pages.

Let's focus on the PHP code block section in the middle of the index.php. We're going to have a little bit of difficulty at first:

I'll go through the code again:


In line 12th of the code, we wrote the first SQL statement in our project, and we filtered all the content out of the page table, if that was the case in the database:


We see that the page table contains all the types of articles, denoted by code, and each type of description is stored accordingly.

We then use the mysql_query function to save the result of the Select to a variable Pages_result :

$pages _result = mysql_query ($pages _sql);

Then, looking at line 17th of the code , in the while statement, we used a function Mysql_fetch_assoc to store the results of the search in a dictionary.

If you google this function, you will find in the MySQL function, a total of 3:

Mysql_fetch_row, Mysql_fetch_array and MYSQL_FETCH_ASSOC

Row can only use subscript 0,1,2 to select the contents of different columns, and assoc can be selected by the database column name, array can be used numbers, can also use key to find.

So the first time the while statement is a variable pages inside is actually a key and value pair, key is code and description, value is news and ' the top news stories from around the World ';

So, we can understand how the 18, 19-row SELECT statement is composed, right? Do not understand can leave a message, we learn together!

Through the while statement loop, we get 3 SELECT statements in turn, and select all the stories that are categorized by page from the stories table



In this way, we show the first story of the different page (because if is not while, only the relevant content of the first stories is shown);

Then we show the corresponding picture through the resize_image.php , this we put in the next chapter to introduce, including some small tips in the database and several links inside the header, we all put to later introduction.


This code to write down, in the browser to enter our URL, you can display the corresponding content. Of course, if you click on the link above will prompt the wrong, we slowly to improve it.


Step by step to teach you to build your site with Php+mysql Home & database connections

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.