How to implement a PHP framework series of articles [6] mysql database, _PHP tutorial

WBOY
Release: 2016-07-12 08:57:59
Original
848 people have browsed it

How to implement a php framework series of articles [6] mysql database,

Issues to consider when implementing a mysql database package

  1. Ease of use

Adopt direct sql statement operation method. As long as you can write SQL statements, there will be no other learning costs.

The dba auxiliary encapsulation class provided by the uctphp framework will be hard to put down after using it.

Do you need to explicitly initialize and connect to the database before use? Of course not.

Until the first sql statement is executed, the database will not be connected, or even a new db object will not be created.

dba will connect to the database at the appropriate time and perform initial character encoding operations.

Query statement. There is no need for a new query constructor and it does not provide a chained operation method, which is so complex and inefficient.

dba provides the following query auxiliary functions.

1 2 3 4 5 6 7 8 9 10 11 12 //读一个值 Dba::readOne($sql); //读一行 Dba::readRowAssoc($sql); //读所有行 Dba::readAllAssoc($sql); //读所有行的第一列 Dba::readAllOne($sql);   //在实际业务场景中,经常会有分页读取部分数据的情况。 //只要一个函数即可返回指定页码的数据内容和数据总条数 Dba::readCountAndLimit($sql$page$limit);

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

Write sentences. Why should we distinguish between read and write? Obviously it can be extended to control read and write separation, double writing and other functions.

Today, with various cloud databases and database middleware, implementation at the database layer is a better choice.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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. Affairs

Use pdo to support transactions

1 2 3 Dba::beginTransaction(); Dba::commit(); Dba::rollBack();

3. Long time running

In some long-running scenarios such as swoole services, background workers, etc., the database connection may time out.

When the database connection is found to have timed out, the DBA will automatically try to reconnect.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1105555.htmlTechArticleHow to implement a php framework series of articles [6] mysql database, issues that need to be considered when implementing a mysql database package and easy to use The direct SQL statement operation method is adopted. As long as you can write...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!