Data Access Object mode describes how to create objects that access the data source transparently. Design a BaseDao base class in the scenario to implement basic query, insert, and update operations in the database. in actual use, inherit BaseDao, and you can directly call the base class... syntaxHighlighter. all (
Data Access Object mode
The Data Access Object mode describes how to create objects that access the data source transparently.
Scenario Design
Design a BaseDao base class to implement the basic query, insert, and update methods for database operations.
In actual use, you can directly call the base-class database operation method by inheriting BaseDao.
Code: BaseDao database operation base class
[Php]
// Data Access Object mode
// Remove the database access layer as a public access interface, which is convenient for users. it is a common design mode in php.
Class BaseDao {
Private $ db;
Public function _ construct ($ config ){
$ This-> db = mysql_connect ($ config ['user'], $ config ['pass'], $ config ['host']);
Mysql_select_db ($ config ['database'], $ this-> db );
}
Public function query ($ SQL ){
Return mysql_query ($ SQL, $ this-> db );
}
}
Code: data operations on UserDao user data tables, inherited from BaseDao
[Php] www.2cto.com
Include ("UserDao. php ");
Class UserDao extends BaseDao {
Public function addUser (){
$ SQL = "INSERT INTO user (username) VALUES ('initphp ')";
Return $ this-> query ($ SQL );
}
}
$ UserDao = new UserDao;
$ UserDao-> addUser ();
Author: initphp