PHP framework series (6) mysql database methods,
Considerations for implementing a mysql database Encapsulation
1. ease of use
Use a direct SQL statement. As long as SQL statements are written, there will be no other learning costs.
The dba-assisted encapsulation class provided by the uctphp framework is easy to use.
Do you need to display initialization and connection to the database before use, of course, no.
The database is not connected or even a new db object until the first SQL statement is executed.
Dba will connect to the database and perform initialization character encoding at the right time.
Query statement. The new query constructor is not required and does not provide chain operations, which is complex and inefficient.
Dba provides the following query auxiliary functions.
// Read a value Dba: readOne ($ SQL); // read a row Dba: readRowAssoc ($ SQL); // read all rows Dba :: readAllAssoc ($ SQL); // read the first Dba column of all rows: readAllOne ($ SQL); // in actual business scenarios, partial data is read by page. // Return the data content and total number of data entries of the specified page number using one function. Dba: readCountAndLimit ($ SQL, $ page, $ limit );
Ps: The preceding functions provide a map function to process each row of the returned array.
Write statement. Why do we need to distinguish between read and write? Obviously, it can be extended to control read/write splitting, dual-write, and other functions.
Today, with a variety of cloud databases and database middleware, implementing at the database layer is a better choice.
Dba: write ($ SQL);/* directly inserting or updating an array in the kv format will automatically escape the value and support array-type values. If you write your own SQL statements, you must use addslashes or mysql_real_escape_string to ensure security */Dba: insert ($ table, $ insert); Dba: update ($ table, $ update, $ where);/* it is more efficient to insert data in batches. Of course, you should use array_chunk to insert data in batches. */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 the swoole service and backend worker, database connection times out.
When the database connection times out, the dba automatically tries to reconnect.
The above content is a series of articles (6) about the PHP framework (6) mysql database method, which I hope to help you!
Articles you may be interested in:
- Php uses mysqli to add data to the database
- Use the mysqldump command in PHP to export the database.
- How to split 0.1 billion million pieces of data into Mysql databases (PHP)
- Php page, mysql database to UTF-8 garbled, UTF-8 encoding problem summary
- Mysql database connection and user management using php