thinkphp model use of database additions and deletions (v)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.