PHP thinkphp Data Manipulation curd

Source: Internet
Author: User

Two data sheets

Here's how:

/********************** Database Operation ********************/
Add Data//////////

Populating data
$NewUser ["username"]= "Zhangsan";
$NewUser ["pwd"]= "123";
$NewUser ["Sex"]=1;
$NewUser ["Age"]=23;
$NewUser ["Type"]=1;

instance table pair Sex
$User =m ("User");
The Add method adds a piece of data to the table and returns the inserted index value
$result = $User->add ($NewUser);

Insert Data End///////


Find Data//////////

Single Query
$User =m ("User");
$result = $User->where ("Sex=1")->find ();
foreach ($result as $key = = $value) {
echo $key. " ==> ". $value." <br/> ";
}

More than one query
$User =m ("User");
$result = $User->where ("Sex=1")->select ();

foreach ($result as $item) {
foreach ($item as $key = = $value) {
echo $key. " ==> ". $value." <br/> ";
}
echo "
}

Find Data End//////////


Update////////

$User =m ("User");
Fill in the data you want to change
$NewData ["username"]= "Wanghe";
Define constraints and populate the data, then save, return the changed index value
$result = $User->where ("id=1")->data ($NewData)->save ();
echo $result;

Update End////////


Delete Operation///////////

$User =m ("User");
Define the constraint, return the changed index value
$reslut = $User->where ("id=1")->delete ();
Echo $reslut;

Delete Operation End///////////

Simple paging Operation//////////
$User =m ("User");
$result = $User->page ("0". ", 2")->select ();
Var_dump ($result);
Simple paging operation End//////////


Transaction Operation/////////////
$User =m ("User");
$User->starttrans ();//Start transaction
$reslut 1= $User->query ("INSERT into User (username) VALUES (' wangwu1 ')");//executable statement
$reslut 2= $User->query ("INSERT into User (username2) VALUES (' Wangwu2 ')");//non-executable statement
if ($reslut 1>0&& $reslut 2>0) {
$User->commit ();//Submit data
}else{
$User->rollback ();//Rollback
}
Transaction Operation End/////////////



Multi-Table Query/////////////
$User =new Model ("User");
$reslut = $User->table (' User,zhuwu ')->where (' User.type = Zhuwu.typeid ')->select ();
Var_dump ($reslut);
Multi-table Query end////////////

PHP thinkphp Data Manipulation curd

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.