Laravel Framework MySQL database-Basic use

Source: Internet
Author: User

Change and delete

After you have configured the database connection, you can use the DB class to query.

Inquire

$results = Db::select (' select * from users where id =? ', array (1));

The Select method returns the result of the query in the form of an array.

Insert

Db::insert (' INSERT into users (ID, name) VALUES (?,?) ', Array (1, ' Dayle '));

Update

Db::update (' update users set votes = + WHERE name =? ', Array (' John ') ');

Delete

DB::d elete (' Delete from users ');

Note: The update and DELETE statements return the number of rows affected.

General statements

Db::statement (' drop table users ');

Listening to the query event

Db::listen (function ($sql, $bindings, $time) {    //});
Database transactions
Db::transaction (function () {    db::table (' users ')->update (Array (' votes ' = 1)];    Db::table (' posts ')->delete ();});

Note: Any exception in the transaction closure will cause the transaction to be automatically rolled back.

Sometimes you might want to start your own business:

Db::begintransaction ();

You can also roll back the transaction yourself:

Db::rollback ();

Finally, you can commit the transaction through the Commit method:

Db::commit ();
Multiple Database connection access

When you use multiple database connections, you can access them through the Db::connection method:

$users = db::connection (' foo ')->select (...);

You can access the native connection via the PDO instance:

$pdo = Db::connection ()->getpdo ();

Sometimes you need to re-connect the database:

Db::reconnect (' foo ');

If you want to disconnect because you exceed the maximum number of connections for the PDO instance, use the Disconnect method:

DB::d isconnect (' foo ');
Query log

By default, Laravel uses logging to log all database query operations that are currently requested, but some times, such as inserting large amounts of data, can cause an application to use excessive memory. In order to close the log, you can use the Disablequery method:

Db::connection ()->disablequerylog ();

To get query logging, you can use the Getquery method:

$queries = Db::getquerylog ();




Reference: http://nonfu.me/p/10911.html, Http://nonfu.me/job

Laravel Framework MySQL database-Basic use

Related Article

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.