Php Framework Development 4 (DAO layer) _ PHP Tutorial

Source: Internet
Author: User
Php Framework Development 4 (DAO layer ). Website development inevitably needs to store some data, but the methods for storing the data in various languages are basically: first open the connection, then execute the SQL statement to obtain the completion result, website development inevitably needs to store some data, but the methods for storing the data in various languages are basically: first open the connection, then execute the SQL statement, get the result, and close the connection.

So what methods does PHP use to operate databases?

[Php]
$ Connect = mysql_connect ($ host, $ user, $ pass );
Mysql_select_db ($ dbname );
$ Result = mysql_query ($ query );
While ($ row = (mysql_fetch_array ($ result ))){
Print_r ($ row );
}
You can use the preceding operations to operate databases. This is enough for the industry that embeds PHP scripts in HTML, but when we want to add, delete, modify, and query a large number of requests, we must reuse the preceding scripts multiple times. This mode will appear.

DAO http://blog.csdn.net/tomyjohn/article/details/7675770

The above pattern is implemented as the DAO layer for the moment, but the real significance lies in our MVC framework, which can be used to operate. Next we will create a new controller

[Php]
Class index {

Public function demo (){
$ Art = new article ();
$ Art-> title = 'title ';
$ Art-> content = 'content ';
$ Art-> save ();
Echo $ art-> id;
View: show('index.htm ');
}
}

Create a new table named "si_article" in the database ",

[SQL]
Drop table if exists 'Si _ article ';
Create table 'Si _ article '(
'Id' int (8) not null AUTO_INCREMENT,
'Title' varchar (255) default null,
'Content' text,
Primary key ('id ')
) ENGINE = InnoDB AUTO_INCREMENT = 2 default charset = gbk;

Create an "article. php" in the "modules" folder"

[Php]
Class article extends dao {
Protected $ table = _ CLASS __;
}
When we execute the controller above, we can see the ID of the newly inserted data.

In this way, we can easily implement an online message function.

Now, almost all MVC has been described. you can use it for development. of course, it is a complete but not rich MVC. There are still many things to consider. For example, paging, cache, and security. But this does not affect my love for this wheel.

Success ,...

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.