Implementing the PHP Framework series article (6) MySQL database method, _php tutorial

Source: Internet
Author: User
Tags bulk insert php framework

Implementing the PHP Framework series article (6) MySQL database method,


Issues to consider in implementing a MySQL database encapsulation

1. Ease of Use

Use direct SQL statement operations. As long as the SQL statements are written, there will be no additional learning costs.

The uctphp Framework provides a DBA-assisted wrapper class that will be put down after the meeting.

Need to display the initialization and connect to the database before use, of course, not required.

The database is not connected until the first SQL statement is executed, not even a new DB object.

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

Query statements. There is no need for a new query constructor or a chained operation, which is complex and inefficient.

The DBA provides the following query helper functions.

Read a value dba::readone ($sql);//Read a line DBA::READROWASSOC ($sql);//Read all Rows Dba::readallassoc ($sql);//Read the first column of all Rows Dba::readallone ( $sql);//In real business scenarios, there is often a case of paging through part of the data. Returns the data content and the total number of data bars for a specified page number as long as a function dba::readcountandlimit ($sql, $page, $limit);

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

Write the statement. Why to distinguish between read and write, obviously can be extended to control read-write separation, double write and other functions.

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

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

2. Business

Using PDO to support transactions

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


3. Long Running time

In some cases where long running scenarios such as Swoole service, background worker, etc. may occur, the database connection time-out is possible.

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

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

Articles you may be interested in:

    • How PHP uses mysqli to add data to a database
    • PHP uses the mysqldump command to export the database
    • 100 million how data is divided into 100 tables in MySQL database (PHP)
    • PHP page, MySQL database turn utf-8 garbled, Utf-8 coding problem Summary
    • PHP implementation of MySQL database connection operation and user management

http://www.bkjia.com/PHPjc/1106122.html www.bkjia.com true http://www.bkjia.com/PHPjc/1106122.html techarticle Implementing the PHP Framework series article (6) MySQL database method to implement a MySQL database encapsulation needs to consider the question 1. The use of direct SQL statement operation is convenient. Whenever you write a SQL statement ...

  • 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.