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