First acquaintance thinkphp (4)

Source: Internet
Author: User

This content is how the database is used

Because in the first chapter is to take the index this file write database access, this experiment I use the file is the system default home

The configuration of the content is also there to mention that no re-stamp map.

The database builds 3 lines of content (using the SQLyog to connect) as recommended in the manual

Now think of this when configuring the databas.php file.

Thinking about it at that point, I think it would probably be able to reference the database by module, not to make the structure chaotic

To access the database, add

Use Think\db

The next step is to show the additional deletions

<?phpnamespace App\index\controller; UseThink\controller; Usethink\db;classIndexextendscontroller{ Public functionindex () {$data= Db::execute (' INSERT into Think_data (ID, data, status) value (7, "Sijidou4", 1) '); Var_dump($data); $data= Db::execute (' UPDATE think_data set data = ' green beans ' where id = 4 '); Var_dump($data); $result= Db::query (' SELECT * from Think_data '); Dump ($result); Echo"<br/>"; $data= Db::execute (' DELETE from think_data where id = 7 '); $result= Db::query (' SELECT * from Think_data '); Dump ($result); Echo"Hello World"; return ; }}

Execute operation, if the result string is output with Var_dump, "int ()" is displayed.

The query operation causes the result to return an array to display the contents of the table field.

The code here can be streamlined to remove the execute operation (since the operation does not require a variable or output), the code is as follows

<?phpnamespace App\index\controller; UseThink\controller; Usethink\db;classIndexextendscontroller{ Public functionindex () {Db:: Execute (' INSERT into Think_data (ID, data, status) value (7, "Sijidou4", 1) '); Db:: Execute (' UPDATE think_data set data = ' green beans yes ' where id = 4 '); $result= Db::query (' SELECT * from Think_data '); Dump ($result); Echo"<br/>"; Db:: Execute (' DELETE from think_data where id = 7 ')); $result= Db::query (' SELECT * from Think_data '); Dump ($result); Echo"Hello World"; return ; }}

Learned a sentence of SQL command

// 清空数据表Db::execute(‘TRUNCATE table think_data‘);

可以用DB::connect(数据库类型//账号:密码@地址:端口/库名#编码)来重新打开新的数据库。

It is convenient to open different databases in this way.

$db 1 = db::connect (' db1 '); $db 2 = db::connect (' DB2 '); $db 1->query (' select * from think_data where id = 1 '); $db 2->query (' select * from think_data where id = 1 ');

Use [] to implement dynamic binding to protect security

For example

Db::execute (' INSERT into Think_data VALUES (?,?,?) ', [7, ' AAA ', 1]);

Db::execule (' INSERT into Think_data VALUES (: ID,:d ata,: status), [' id ' = + 7, ' data ' = ' aaa ', ' status ' = 1] ');

THINKPHP5 also provides a query constructor that represents each step with a function, and connects each step with a

//This code is official document (I prefer the original ecological code above)
//Inserting RecordsDb::table (' Think_data ') ->insert ([' id ' = ' + ', ' name ' = ' thinkphp ', ' status ' = 1]);//Update RecordDb::table (' Think_data ') ->where (' id ', 18) ->update ([' name ' = = ' Hello ']);//Querying Data$list= db::table (' Think_data ') ->field (' Name,email ') ->where (' id ', 18) -Select ();//Delete DataDb::table (' Think_data ') ->where (' id ', 18) ->delete ();

Note that using select () to query, the select must be placed at the end, before the object to make a select the information.

The first mention of prefix, the prefix of this setting table, can have the following effect here

Db::table (' Think_data ') is equivalent to Db::name (' data ');

There is not much content, it is here today.

 

First acquaintance thinkphp (4)

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.