Home >Backend Development >PHP Tutorial >How to implement a php framework series of articles mysql database

How to implement a php framework series of articles mysql database

WBOY
WBOYOriginal
2016-07-29 09:00:441162browse

Issues to consider when implementing a mysql database package

  1. Ease 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.

1

2

3

4

5

6

7

8

9

10

11

12

//Read one Value

Dba::readOne($sql);

//Read one row

Dba::readRowAssoc($sql);

//read All rows

Dba::readAllAssoc($sql);

//Read the first column of all rows

Dba::readAllOne($sql);

//In actual business scenarios, partial data is often read in pages.

//Just one function can return the data content and total number of data items of the specified page number

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

Dba::write($sql);

/* Directly insert or update array array in kv form

will automatically escape the value, and also support array type value.

If you write sql statements yourself, be careful to use addslashes or mysql_real_escape_string to ensure safety

*/

Dba::insert($table, $insert );

Dba::update($table, $update, $where);

/*

Higher efficiency for inserting data in batches

​​ Of course, too many rows should be inserted in batches using array_chunk.

*/

Dba::insertS($table, $inserts);

2. Transaction

Use pdo to support transactions

1

2

3

Dba::beginTransaction();

Dba::commit();

Dba::rollBack();

3. Long-running

In some scenarios that require long-running such as swoole services, background workers, etc., the database connection may time out.

When it is found that the database connection has timed out, the DBA will automatically try to reconnect.

The above introduces how to implement a mysql database in the PHP framework series of articles, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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
Previous article:php data typesNext article:php data types