Use thinkphp (3.2.3) to manipulate the database, first to connect to the database. We need to write a configuration file for a database, and thinkphp will automatically connect to the database based on the configuration file. The model file is not customizable, and the built-in solution solves the problem. The configuration file is written under directory application\home\conf\config.php:
<? PHP return Array (//' config item ' = = ' config value' ' db_type ' = ' mysql ',//database type ' db_host ' = ' 127.0.0.1 ',//server address ' DB ') _name ' + ' jiu151231 ',//database name ' Db_user ' + ' root ',//username ' db_pwd ' + ', '//password ' db_port ' = ' 3306 ',//Port ' db_ ' PREFIX ' = + ',//database table prefix );
Here, the preparation is completed. Which, that database table prefix, online about the default various statements, my default is empty. Estimate the other prefixes and open phpmyadmin to see it.
The curd operation code is in the directory application\home\controller\indexcontroller.class.php, which is the controller file. The following are examples of table objects that are instantiated using the M method.
C
Inserting data
<? phpnamespace Home\controller; Use Think\controller; class extends Controller { publicfunction index () { $jiu 0=m (' myguests ' ); $j 0 [' FirstName ']= ' LV '; $j 0 [' LastName ']= ' oe '; $jiu 0->add ($j 0);} }
Inserting more than one data
<?phpnamespace Home\controller; UseThink\controller;classIndexcontrollerextendsController { Public functionindex () {$jiu 0=m (' myguests '); $list[]=Array(' firstname ' = ' li ', ' lastname ' = ' Weifeng '); $list[]=Array(' firstname ' = ' Liu ', ' lastname ' = ' Tian '); $jiu 0->addall ($list); }}
R
Read a single record Find method returns the first qualifying record
<? phpnamespace Home\controller; use Think\controller; class Indexcontroller extends Controller { Span style= "color: #0000ff;" >public function index () { $gu =m (' myguests ' ); $data = $gu ->where (' Firstname= "LV" ')->find (); Dump ( $data ); }}
<? phpnamespace Home\controller; Use Think\controller; class extends Controller { publicfunction index () { $gu=m (' myguests ') ); $gu->where (' firstname= ' LV '),find (); Dump ($gu, data ());} }
Read multiple Data Select method if the query is faulted, the return value of select is False and NULL is returned if the query result is NULL, otherwise a two-dimensional array is returned.
<? phpnamespace Home\controller; Use Think\controller; class extends Controller { publicfunction index () { $gu = M ("myguests" ); $list $gu->where (' firstname= ' LV ')->order (' id ')->limit (5)->select (); // returns 5 Data Dump ($list);} }
Read field values
<? phpnamespace Home\controller; use Think\controller; class Indexcontroller extends Controller { Span style= "color: #0000ff;" >public function index () { $gu = M ("myguests" ); $list = $gu ->where (' id=5 ')- >getfield (' FirstName ' ); Dump ( $list ); }}
<? phpnamespace Home\controller; use Think\controller; class Indexcontroller extends Controller { Span style= "color: #0000ff;" >public function index () { $gu = M ("myguests" ); $list = $gu ->getfield (' id ', Span style= "color: #0000ff;" >true $list ); }}
<? phpnamespace Home\controller; Use Think\controller; class extends Controller { publicfunction index () { $gu = M ("myguests" ); $list $gu->getfield (' Id,firstname ', 5);//returns multiple columns, 5 records dump ($list); }}
U
Save method
<? phpnamespace Home\controller; Use Think\controller; class extends Controller { publicfunction index () { $gu = M ("myguests" ); $data [' FirstName ']= ' Liu '; $data [' LastName ']= ' hl '; $gu->where (' id=5 ')->save ($data); }}
<? phpnamespace Home\controller; Use Think\controller; class extends Controller { publicfunction index () { $gu = M ("myguests" ); $gu->firstname= ' Liu '; $gu->lastname= ' hl '; $gu->where (' id=6 '),Save ();} }
Update fields
<? phpnamespace Home\controller; Use Think\controller; class extends Controller { publicfunction index () { $gu = M ("myguests" ); $gu->where (' id=6 ')->setfield (' FirstName ', ' Wang ');} }
Update multiple fields
<? phpnamespace Home\controller; Use Think\controller; class extends Controller { publicfunction index () { $gu = M ("myguests" ); $data=array(' firstname ' = ' LV ', ' lastname ' = ' Huan '); $gu->where (' id=1 ')->setfield ($data); }}
The system also provides Setinc and Setdec methods for updating statistical fields, which are often referred to as numeric types.
$User // instantiating a User object $User // users ' points plus 3 $User // users ' points plus 1 $User // User's points minus 5 $User // user's points minus 1
Starting with the 3.2.3 version, the Setinc and Setdec methods support deferred updates, using the following:
$Article // instantiating a Article object $Article // article reading plus 1 $Article // article read count plus 1, and delay 60 seconds Update (write)
D
Delete method
<? phpnamespace Home\controller; Use Think\controller; class extends Controller { publicfunction index () { $gu = M ("myguests" ); $gu->delete (2); // Delete the record for primary key 2 }}
The Delete method can either delete individual data or delete multiple data, depending on the deletion criteria, such as:
$User // instantiating a User object $User // Delete user data with ID 5 $User // Delete user data with primary key 1, 2, and 5 $User // Delete all user data with FirstName as "LV"
thinkphp curd operation on MySQL