Home  >  Article  >  Backend Development  >  Implementing PHP framework series of articles (6) mysql database method, _PHP tutorial

Implementing PHP framework series of articles (6) mysql database method, _PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:57:56925browse

A series of articles on implementing PHP framework (6) mysql database method,

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.

//读一个值
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.

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

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.

The above content is the PHP framework series article (6) mysql database method introduced by the editor to you. I hope it will be helpful to everyone!

Articles you may be interested in:

  • How to add data to the database using mysqli in PHP
  • PHP uses the mysqldump command to export the database
  • 100 million pieces of data How to divide 100 tables into Mysql database (PHP)
  • php page, convert mysql database to utf-8 garbled code, summary of utf-8 encoding issues
  • php realizes mysql database connection operation and user management

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1106122.htmlTechArticleA series of articles on implementing PHP framework (6) mysql database method, issues to consider when implementing a mysql database encapsulation 1. Use Convenience adopts direct sql statement operation method. As long as you can write sql statements...
Statement:
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