Implementation of PHP Framework series (6) MySQL Database method _php Example

Source: Internet
Author: User
Tags bulk insert dba php framework

Issues to be considered in implementing a MySQL database encapsulation

1. Ease of Use

The direct SQL statement is used to operate the method. As long as you write SQL statements, there will be no other learning costs.

The uctphp Framework provides DBA helper encapsulation classes that will be put down after the meeting.

Do you need to show initialization and connect to the database before you use it, of course not.

The database will not be connected until the first SQL statement is executed, or it will not even be new to a DB object.

The DBA will connect to the database at the right time and perform the initialization character encoding operation.

The query statement. You don't need new. A query builder does not provide a chain operation, so it is complex and inefficient.

The following query helper functions are provided by the DBA.

Read a value
dba::readone ($sql);
Read a line of
Dba::readrowassoc ($sql);
Read all row
Dba::readallassoc ($sql);
Read the first column of all Rows
dba::readallone ($sql);
In real-world business scenarios, there are often pages that read part of the data.
//As long as a function can return the specified number of data content and data total number
of bars Dba::readcountandlimit ($sql, $page, $limit);

PS: The above functions can provide a map function to process each row of the returned array.

Write statements. Why to distinguish between read and write, obviously can be extended to control read and write separation, dual write functions.

In a variety of cloud database and database middleware today, the implementation of the database layer is a better choice.

Dba::write ($sql);
/* directly inserting or updating the array arrays in the KV form
automatically escapes value and also supports array-type values.
If you write your own SQL statements should pay attention to the use of addslashes or mysql_real_escape_string to ensure security/
Dba::insert ($table, $insert); 
Dba::update ($table, $update, $where);
/*
more efficient for BULK insert data of
course, excessive number of rows should be inserted in batches with Array_chunk.
*/
Dba::inserts ($table, $inserts);

2. The transaction

Using PDO support transactions

Dba::begintransaction ();
Dba::commit ();
Dba::rollback ();


3. Run for a long time

The database connection timeout may occur in some long-running scenarios such as swoole services, background worker, and so on.

When a database connection timeout is found, the DBA will automatically attempt to reconnect.

The above content is small to introduce the PHP framework series of articles (6) MySQL database method, I hope to help you!

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.