Issues to consider when implementing a mysql database package
1. Convenience of use
Use direct sql statement operation. 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, you will not be able to put it down after using it.
Does it need to be explicitly initialized and connected to the database before use? Of course not.
Until the first sql statement is executed, it will not connect to the database, or even create a new db object.
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. Transactions
Use pdo to support transactions
Dba::beginTransaction(); Dba::commit(); Dba::rollBack();
3. Long-running
In some scenarios that require long-running such as swoole services, background workers, etc., Database connection timeout may occur.
When it is found that the database connection has 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!
The above introduces the 6mysql database method of implementing the PHP framework series of articles, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.