About database operations for THINKPHP5

Source: Internet
Author: User

1. Database Configuration



2. Query execute original ecological SQL statement Additions and deletions

$result = Db::execute (' INSERT into log (user_id, IP) ' VALUES (1, 11231) ');d UMP ($result); $result = Db::query (' select * from lo G '); Echo ' <pre> '; var_dump ($result);


3. parameter-bound named placeholder bindings

$str = ' INSERT into log (user_id, IP) VALUES (?,?) '; $result = Db::execute ($str, [1, ' 12312 ']), $result = Db::query (' select * from log where id =? ', [4]);//Placeholder Db::execute (' ins ert into log (user_id, IP) VALUES (: user_id,: IP) ', [' user_id ' =>12, ' ip ' = ' 5555 ']);

4. Query Builder


Added: db::table (' log ')->insert ([' user_id ' =>1, ' ip ' = ' 654321 ']);//update db::table (' log ')    ->where (' id ') ,    ->update ([' user_id ' =>123]);//query data $list = db::table (' log ')    ->where (' id ', ')->select    ();//delete data db::table (' log ')    ->where (' id ', ten)    ->delete ();

To query a table without prefixing the method:

Db::name (' log ')->insert ([' user_id ' =>44, ' IP ' =>5555]);

5. DB chain-operated

Ways to support chained queries:

Method name

Description

Select

Querying the database

Find

Querying individual records

Insert

Insert record

Update

Update record

Dalete

Delete Record

value

Query value

column

Query column

Chunk

Block query

Count

Aggregate query

6. Things to support

Automatic control of Things db::transaction (function () {    db::table (' log ')->delete (2);    Db::table (' log ')->insert ([' user_id ' =>123]); /manually control the submission of things//Start things Db::starttrans (); try {    db::table (' log ')        ->where (2);    Db::table (' log ')        ->insert ([' user_id ' = 213]);    Db::commit ();} catch (Exception $e) {    db::rollback ();}

This article explains about the THINKPHP5 database operation, more relevant content please pay attention to the PHP Chinese network.

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.