How to implement a PHP framework series article "6" MySQL Database

Source: Internet
Author: User
Tags php framework

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.

123456789101112 //读一个值Dba::readOne($sql);//读一行Dba::readRowAssoc($sql);//读所有行Dba::readAllAssoc($sql);//读所有行的第一列Dba::readAllOne($sql);//在实际业务场景中,经常会有分页读取部分数据的情况。//只要一个函数即可返回指定页码的数据内容和数据总条数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.

123456789101112131415 Dba::write($sql);/* 直接插入或更新kv形式的array数组会自动对value进行转义,也支持array类型的值。如果自己写sql语句要注意使用addslashes或mysql_real_escape_string来保证安全*/Dba::insert($table$insert);    Dba::update($table$update$where);/*    对批量插入数据有更高的效率    当然过多的行数应该用array_chunk来分批插入。*/Dba::insertS($table$inserts);

2. Business

Using PDO to support transactions

123 Dba::beginTransaction();Dba::commit();Dba::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.

How to implement a PHP framework series article "6" MySQL database

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.