Home > Backend Development > PHP Tutorial > 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
Release: 2016-07-12 08:57:56
Original
936 people have browsed it

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);
Copy after login

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);
Copy after login

2. Affairs

Use pdo to support transactions

Dba::beginTransaction();
Dba::commit();
Dba::rollBack();
Copy after login


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...
Related labels:
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