Issues to be considered in implementing a MySQL database encapsulation
1. Ease of Use
The direct SQL statement is used to operate the method. As long as you write SQL statements, there will be no other learning costs.
The uctphp Framework provides DBA helper encapsulation classes that will be put down after the meeting.
Do you need to show initialization and connect to the database before you use it, of course not.
The database will not be connected until the first SQL statement is executed, or it will not even be new to a DB object.
The DBA will connect to the database at the right time and perform the initialization character encoding operation.
The query statement. You don't need new. A query builder does not provide a chain operation, so it is complex and inefficient.
The following query helper functions are provided by the DBA.
Read a value
dba::readone ($sql);
Read a line of
Dba::readrowassoc ($sql);
Read all row
Dba::readallassoc ($sql);
Read the first column of all Rows
dba::readallone ($sql);
In real-world business scenarios, there are often pages that read part of the data.
//As long as a function can return the specified number of data content and data total number
of bars Dba::readcountandlimit ($sql, $page, $limit);
PS: The above functions can provide a map function to process each row of the returned array.
Write statements. Why to distinguish between read and write, obviously can be extended to control read and write separation, dual write functions.
In a variety of cloud database and database middleware today, the implementation of the database layer is a better choice.
Dba::write ($sql);
/* directly inserting or updating the array arrays in the KV form
automatically escapes value and also supports array-type values.
If you write your own SQL statements should pay attention to the use of addslashes or mysql_real_escape_string to ensure security/
Dba::insert ($table, $insert);
Dba::update ($table, $update, $where);
/*
more efficient for BULK insert data of
course, excessive number of rows should be inserted in batches with Array_chunk.
*/
Dba::inserts ($table, $inserts);
2. The transaction
Using PDO support transactions
Dba::begintransaction ();
Dba::commit ();
Dba::rollback ();
3. Run for a long time
The database connection timeout may occur in some long-running scenarios such as swoole services, background worker, and so on.
When a database connection timeout is found, the DBA will automatically attempt to reconnect.
The above content is small to introduce the PHP framework series of articles (6) MySQL database method, I hope to help you!