PHP framework development four (DAO layer)_PHP tutorial

WBOY
Release: 2016-07-13 17:53:12
Original
1219 people have browsed it

Website development inevitably requires the storage of some data, but the way of storing this data in various languages ​​is basically: first open the connection, then execute the SQL statement, obtain the result, and close the connection.

So what methods does PHP have when operating a database?

[php]
$connect = mysql_connect($host,$user,$pass);
mysql_select_db($dbname);
$result = mysql_query($query);
while($row = (mysql_fetch_array($result))){
Print_r($row);
}
Use the above operations to operate the database. For industries that embed PHP scripts in HTML, this is enough, but when we need to perform a large number of additions, deletions, modifications, and checks, we must reuse the above script multiple times. Then this pattern will appear

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

For now, let’s use the above model as the DAO layer, but the real significance lies in that our MVC framework can be operated in this way. Next we 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 in the database named "si_article",

[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 a new "article.php" in the "modules" folder

[php]
class article extends dao{
protected $table = __CLASS__;
}
When we execute the above controller, we can see the ID of the data just inserted.

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

Up to this point in this framework, almost all MVCs have been described. You can use it to develop. Of course, it is a complete but not rich MVC. There is much more to consider. For example, paging, caching, security, etc. But that doesn't affect my love for this wheel.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478061.htmlTechArticleWebsite development inevitably requires the storage of some data, but the ways of storing this data in various languages ​​are basically the same Yes: Open the connection first, then execute the SQL statement to get the result,...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template