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!