Original: thinkphp model use of database additions and deletions (v)
The thinkphp model uses
Connect directly to the database, but it has to be configured in the configuration file first.
Class Indexaction extends Action {
Public Function Adddb () {
corresponding to the database table, be sure to correspond to the database table name size okay
$user =new Model (' Leyangjun ');
$user = M (' Leyangjun '); equals the new Model above ()
$arr = $user->select ();
Var_dump ($arr);
}
To the database of additions and deletions to change the operation (abbreviation: curd)
One: Add-C Create $m->add ()
$m =new Model (' User ');
$m =m (' User ');
$m, field name = ' Value '; $user->name= ' Yangjun ';
$m->add (); The return value is the new ID number
Second: delete-D delete $m->delete ()
$m =m (' User ');
$m->delete (2); Delete the data with ID 2, and if you do not write 2, all the data will be deleted.
$count = $m->where (' id=2 ')->delete (); Same as above, also delete data with ID 2
The return value is the number of rows affected
if ($count >0) {
$this->success ("Data deleted successfully! ");
}else{
} $this->error ("Data deletion failed! ");
Three: Change-u Update $m->save ()
$m =m (' User ');
$data [' id ']=1;
$data [' name ']= ' Leyangjun '; Change the name value of the id=1 to Leyangjun
$m->save ($data);The return value is the number of rows affected
Four: Check-R Read $m->select ()
$m =new Model (' User '); = = $m =m (' User ');
1:select
$m->select (); Gets all data, returned as an array
2:find
$m->find ($id);Get a single piece of data by default is id=1 data, want to take second find (2)
3:getfield (field name)Get a specific field value
$arr = $m->getfield (' name '); Gets a specific field value that reads the name of the first article by default
$arr = $m->where (' id=2 ')->getfield (' username '); Coherent operation, to id=2 the username value
}
thinkphp model use of database additions and deletions (v)