Create models under the Model folder, file naming rules: Table name Model.class.php
<? phpnamespace Home\model; Use Think\model; class extends model{ }
Three ways to instantiate a database
//calling models under the Model folder Public functionshujuku1 () {$yonghu=New\home\model\yonghumodel;//instantiate the model that you created, (note that the path is fully written) Var_dump($yonghu); } //Direct instantiation of the D method Public functionShujuku2 () {$yonghu=d (' Yonghu ');//directly using the D method, you need a parameter, the table name under the database Var_dump($yonghu); } //The m method is instantiated directly (making the parent class object) Public functionShujuku3 () {$yonghu=m (' Yonghu ');//directly using the M method, you need a parameter, the table name under the database Var_dump($yonghu); }
Querying the database
// query Database Public function Chaxun () { $yonghu=d (' Yonghu '); Var_dump ($yonghu//Select Query method, returns a two-dimensional array indexed as lowercase (only on the last side of the coherent operation) }
Three ways to add data to a database
//Add the way you create arrays Public functionadd1 () {$mode=d ("Yonghu");//instantiate a Database Yonghu table, (convert table to Class) $attr=Array //Array , the index of the array and the database column name one by one correspond ( ' Zhanghao ' =>001, ' Mima ' =>123, ' mingzi ' = ' Liu Big ', ' Xingbie ' and ' men ', ' Shengri ' = ' 1990-07-09 ', ' shijian ' = ' 2016-6-16 14:09:30 ', ' Shengfen ' and ' management ', ' Zhuangtai ' = ' is activated ', ); $mode->add ($attr);//Add to Database } //the way to assign a class member to add Public functionadd2 () {$mode=d (' Yonghu ');//instantiate a Database Yonghu table, (convert table to Class) $mode->zhanghao= ' 002 ';//assigning a class member to a member name corresponding to the database column name one by one $mode->mima= ' 123 '; $mode->mingzi= ' Little Two '; $mode->xingbie= ' man '; $mpde->shengri= ' 1992-09-09 '; $mode->shijian= ' 2016-6-16 14:09:31 '; $mode->shengfen= ' users '; $mode->zhuangtai= ' not activated '; $mode->add ();//Add to Database } //how to automatically collect form member data by adding Public functionadd3 () {$mode=d (' Yonghu ');//instantiate a Database Yonghu table, (convert table to Class) $mode->create ();//the name of the Automatic collection form form and the column name of the database must correspond to one by one $z=$mode->add ();//Add to Database if($z) { $this->success ("Add data successfully! "," Add ", 3);//prompt when adding success } Else { $this->error ("Add failed! "," Add ", 5);//prompt when adding failure } }
Configuration file content to configure when connecting to a database
<?PHPreturn Array( //' Config item ' = ' config value '' Tmpl_l_delim ' = ' <{', ' tmpl_r_delim ' = '}> ', ' Url_model ' =>2, ' show_page_trace ' + =TRUE,/*Database Settings*/' Db_type ' = ' mysql ',//Database Type' db_host ' = ' localhost ',//Server Address' db_name ' = ' index ',//Database name' Db_user ' = ' root ',//User name' Db_pwd ' = ',//Password' Db_port ' = ',//Port' Db_prefix ' = ',//database table Prefixes' Db_params ' =Array(),//Database Connection Parameters' Db_debug ' =TRUE,//SQL log can be logged when database debug mode is turned on' Db_fields_cache ' =true,//Enable field caching' Db_charset ' = ' UTF8 ',//database encoding is UTF8 by default' Db_deploy_type ' = 0,//Database Deployment method: 0 Centralized (single server), 1 distributed (Master-slave server)' Db_rw_separate ' =false,//whether the database reads and writes separate master-slave valid' Db_master_num ' = 1,//number of master servers for read-write separation' Db_slave_no ' = ',//Specify the number from the server);
Error method of shielding system error
, the No. 237 line of the Driver.class.php file under the Tp\thinkphp\library\think\db folder is "$this->error ();" Comment out
99th Day Class PHP TP Framework database query and add