Thinkphp_5 operation of the database

Source: Internet
Author: User

Db::query ();
Db::execute ();

Db::table ()->select (); Returns an empty array when all data, two-dimensional array, result does not exist
Db::table->find (), one data, one-dimensional array, returns NULL if the result does not exist

Db::table->value (); Returns null if the result does not exist
Db::table->column (); Returns a one-dimensional array, if there is a second argument, returns an array with the second number as the identity, and returns null if the result does not exist

Db::table ()-> Table name plus table prefix
Db::name ()-> Ignore table prefixes


Add Data "Array"
Db::name ()->insert (); Returns the number of rows affected
Db::name ()->insertgetid (); Get the last new ID
Db::name ()->insertall (); Insert all data

Update Database "Array"
Db::name ()->where ()->update (); Returns the number of affected rows
Db::name ()->where ()->setfield (' name ', ' millet '); one field of the update data returns the number of rows affected
Db:name ()->where->setinc (' num '); num Field name 1 per increment
Db:name ()->where->setinc (' num ', 5); num Field name 5 per increment
Db::name ()->where ()->setdec (' num '); num field each time self-decrement


Delete
Db::name ()->where ()->delete (); Returns the number of affected rows
If the condition you want to delete is a primary key, you can not write where
Db::name ()->delete (1); Delete a id=1 record


Conditional constructors
Db::name ()->where ()->buildsql (); return SQL statement
Db::name ()->where ("Id=1")->buildsql (); Delivery conditions
Db::name ()->where ("id", 1)->buildsql (); Pass the field name, and the value you want to use
Db::name ()->where ("id", "<>", 1)->buildsql (); Field names, expressions, values to be judged
Db::name ()->where (' id ', ' between ', ' 1,5 ')->buildsql (); IDs between 1-5, including 1 and 5

Db::name ()->where ([' ID ' =>1])->buildsql ();
Db::name ()->where ([' ID ' =>[' in ', [1,2,3,4]]])->buildsql ();

"Two conditions are the relationship of and"
Db::name ()->where (
[' ID ' =>1],
[' Name ' = ' Kaluo ']
)->buildsql ();


EXP is a conditional expression
Db::name ()->where ("id", "EXP", "Not In ()")->buildsql ();

"Two conditions are the relationship of or"
Db::name ()->where ("id", "in", "No")->whereor (' name ', ' Buld ')->buildsql ();

where () is an array, is a string, is a parameter

# Note Information "The letters are compiled into the following symbols, etc." "condition is case insensitive"
# EQ =
# NEQ <>
# LT <
# ELT <=
# GT >
# EGT >=
# between between * and *
# Notbetween not between * and *
# in In (*,*)
# notin NO TIN (*,*)

Expression::
Between
Inch

Chained operation
Db::table ()->where (' id ', ' > ', Ten)->select (); All fields in the queried table

"Field Method"
Db::table ()->where (' id ', ' > ', ten)->field ("Name,id")->select (); The Name,id field in the query table

"Order Method"
Db::table ()->where (' id ', ' > ', ten)->field ("Name,id")->order ("id DESC")->limit (3,5)->select (); Query the Name,id field in the table, flashback sort, starting from the third one, fetch 5

The page Method page (2,5) starts on the second page and displays five articles "
Db::table ()->where (' id ', ' > ', ten)->field ("Name,id")->order ("id DESC")->page (3,5)->select (); Query the Name,id field in the table, flashback sort, start from the third page, fetch 5

"Group Group"
Db::table ()->where (' id ', ' > ', ten)->field ("Name,id")->group ("' Group '")->select (); The Name,id field in the query table, GROUP BY group

Thinkphp_5 operation of the database

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.