-
Three main APIs for connecting php to the mysql database server: mysql, mysqli, and pdo
They are also common extensions. Which of the following is their performance?
Actually, it is better, but it is better in comparison (I like pdo)
- /**
- After reading a lot of related database connections and operations
- A self-summary of database connection in php
- ------------ Hope to be helpful
- */
- // 1 connection type
- /**
- Three major APIs commonly used in php link-MySQL database server: mysql, mysqli, and pdo
- /******** Basic information *********/
- A. API -------------- Application Programming Interface
- The Application Interface (abbreviated as Application Programming Interface) defines classes, methods, functions,
- Variables, and so on. In the PHP application
- APIs required for interacting with databases are usually exposed through PHP extensions (called by terminal php programmers ).
- APIs can be process-oriented or object-oriented. For process-oriented APIs, we call functions to complete
- For object-oriented APIs, we instantiate classes and call methods on the objects obtained after instantiation.
- For these two interfaces, the latter is usually the first choice, because it is more modern and brings us good
- Code structure.
- B. connector ----- "a piece of software code that allows your application to connect to the MySQL database server ".
- When your PHP application needs to interact with a database server, you need to write php code to complete "connect data
- Database server, query database, and other database-related functions. Your PHP application will use
- Software for these APIs, or use intermediate libraries as needed to process the connection between your applications and database servers
- .
- C. Driver
- Software code used to interact with a specific type of database server. The driver may call some libraries,
- For example, the MySQL client library or the MySQL Native driver library. These libraries are used for MySQL database services.
- The underlying protocol for interaction.
-
- The connector and driver terms may not be differentiated.
- In MySQL documentation, the term "driver" is used as a connector package.
- Provides software code for specific database sections.
- D. What is expansion?
- You will also find many other extensions in the PHP document. PHP code is a core and some optional extensions constitute the core functions.
- MySQL-related extensions of PHP, such as mysqli and mysql, are implemented based on the PHP extension framework.
- A typical function of extension is to expose an API to PHP programmers and allow them to extend their functions.
- Of course, some extensions developed based on the PHP Extension Framework do not expose API interfaces to PHP programmers.
- For example, the PDO MySQL driver extension does not expose API interfaces to PHP programmers, but provides an interface to the PDO layer at the upper layer.
- The term API and the extension describe different things, because the extension may not need to expose an API interface to the programmer.
- *******/
- Main APIs provided in PHP for MySQL:
- ■ MySQL extension of PHP
- (Advantages and disadvantages)
- 【
- Design and Development early extensions that allow PHP applications to interact with MySQL databases. Mysql extension provides a process-oriented interface,
- It is designed for MySQL 4.1.3 or earlier versions. Therefore, although this extension can be associated with MySQL4.1.3 or the number of updates
- The database server interacts with each other, but does not support some features provided by the MySQL server later.
- ]
- ■ Mysqli extension of PHP
- Mysqli extension, which is sometimes called MySQL enhanced extension, can be used to use MySQL4.1.3 or to update new advanced features in the version.
- MySQL I extensions are included in PHP 5 and later versions.
- The mysqli extension has a series of advantages, which are mainly improved compared with the mysql extension:
- ■ Object-oriented interface
- ■ Supported prepared statements)
- ■ Multi-statement execution support
- ■ Transaction support
- ■ Enhanced debugging capability
- ■ Embedded service support
- ■ PHP Data object (PDO)
- PHP Data objects are a database abstraction layer specification in PHP applications. PDO provides a unified API
- So that your PHP application does not care about the type of database server system to be connected. That is to say,
- If you use the pdo api, you can seamlessly switch the database server whenever necessary.
- ***********/
- PHP-MySQL is the original Extension for PHP to operate MySQL databases. I of PHP-MySQLi represents Improvement,
- More advanced functions are provided. Extension also increases security.
- The PDO (PHP Data Object) provides an action Layer to operate databases.
- Detailed source reference: http://www.jb51.net/article/28103.htm
- 1. mysql and mysqli
- Mysqli is a new function library provided by php5. (I) indicates improvement, and the execution speed is faster. of course, it is more secure.
- Detailed source reference: http://www.jb51.net/article/28103.htm
- Mysql is a non-persistent connection function, while mysqli is a permanent connection function. That is to say
- Mysql opens a connection process at each link, and mysqli runs mysqli multiple times and uses the same connection process,
- This reduces the server overhead.
- Some friends always report new mysqli ('localhost', usenamer ', 'password', 'databasename') during programming.
- Error, Fatal error: Class 'mysqli' not found in d :\...
- Isn't the mysqli class included in php?
- It is not enabled by default. in win, you need to change php. ini and remove the ones before php_mysqli.dll. in linux, you need to compile mysqli.
- I. Mysqli. dll is an object-based or process-based database that is easy to use. Here are a few examples
- Detailed source reference: http://www.jb51.net/article/28103.htm
- Do more, do less talk:
- */
- Mysql_connect ($ db_host, $ db_user, $ db_password );
- Mysql_select_db ($ dn_name );
- $ Result = mysql_query ("SELECT 'name' FROM 'users' WHERE 'location' = '$ location '");
- While ($ row = mysql_fetch_array ($ result, MYSQL_ASSOC ))
- {
- Echo $ row ['name'];
- }
- Mysql_free_result ($ result );
-
- /**
- In fact, there are some knowledge behind it...
- This method cannot Bind Column. in the previous SQL statement, $ location is prone to SQL Injection.
- Later, I developed mysql_escape_string () (Note: Use it out after 5.3.0) and mysql_real_escape_string ()
- To solve this problem, but in this case, the entire narrative will become complex and ugly, and if there are too many columns, you can imagine what the situation would be...
- Detailed source reference: http://www.jb51.net/article/28103.htm
- */
- $ Query = sprintf ("SELECT * FROM users WHERE user = '% s' AND password =' % s '",
- Mysql_real_escape_string ($ user ),
- Mysql_real_escape_string ($ password ));
- Mysql_query ($ query );
- /**
- PHP-MySQLi has made a lot of progress. in addition to solving the above problems through Bind Column, it also provides multiple support for Transaction, Multi Query,
- The Object oriented style and Procedural style are also provided.
- (The above PHP-MySQL example) two types of writing... And so on.
- Detailed source reference: http://www.jb51.net/article/28103.htm
- */
- $ Mysqli = new mysqli ($ db_host, $ db_user, $ db_password, $ db_name );
- $ SQL = "INSERT INTO 'Users' (id, name, gender, location) VALUES (?, ?, ?, ?) ";
- $ Stmt = $ mysqli-> prepare ($ SQL );
- $ Stmt-> bind_param ('dss', $ source_id, $ source_name, $ source_gender, $ source_location );
- $ Stmt-> execute ();
- $ Stmt-> bind_result ($ id, $ name, $ gender, $ location );
- While ($ stmt-> fetch ())
- {
- Echo $ id. $ name. $ gender. $ location;
- }
- $ Stmt-> close ();
- $ Mysqli-> close ();
- /**
- However, some shortcomings have been found here, such as the Bind Result. this is a bit redundant, but it doesn't matter,
- Because the biggest problem is that this is not an abstract action method, it is a painful start when the backend changes the database...
- So PDO appeared.
- Detailed source reference: http://www.jb51.net/article/28103.htm
- */
- // 2. PDO and mysql
- /*
- PDO is supported only after PHP5.1. it adopts a consistent interface for accessing the database. However, many open-source programs in China are
- Use the function provided by MySQL extension to connect to the database for query. Why is PDO not used by mature PHP systems in China?
- I have asked a few friends why I use PDO. The answer is "fast". will PDO connect to the database quickly? Why use PDO?
- What are the differences between the two methods? First of all, we are still concerned about performance issues. we have written a script to test how to insert 1 million data records into MySQL.
- */
- $ Link = mysql_connect ("localhost", "root", "root") or die ('MySQL connect error ');
- $ Num = 100000;
- $ Dsn = "mysql: host = 127.0.0.1; dbname = performace_test ";
- $ Db = new PDO ($ dsn, 'root', 'root', array (PDO: ATTR_PERSISTENT => true ));
- Mysql_query ('truncate TABLE 'into Mace _ Test'. 'myquery', $ link); // TRUNCATE Table
- $ Query = "insert into 'into Mace _ test '. 'myquery' ('goods _ id', 'Cat _ id', 'Click _ count', 'goods _ number', 'goods _ weight', 'goods _ sn ', 'goods _ name', 'goods _ reason ', 'brand _ name', 'goods _ thumb', 'brand _ id', 'is _ on_sale ', 'wap _ cod ', 'wap _ title', 'wap _ detail', 'wap _ flag', 'wap _ onsale ', 'shop _ price ', 'cost _ price', 'Channel _ rate', 'Channel _ onsale ', 'Add _ time', 'is _ main', 'Last _ Update ', 'brand _ logo ') VALUES ('80', '000000', '65', '000000', '0. 125 ', 'smt000080', 'health', ", 'health100', 'Images/120/thumb_img/Shanghai ')";
- $ Start_time = microtime (true );
- For ($ I = 0; $ I <$ num; $ I ++)
- {
- Mysql_query ($ query, $ link );
- }
- Echo "USE MySQL extension:". (microtime (true)-$ start_time );
- Mysql_query ('truncate TABLE 'into Mace _ Test'. 'myquery', $ link); // TRUNCATE Table
- $ Start_time = microtime (true );
- For ($ I = 0; $ I <$ num; $ I ++)
- {
- $ Db-> exec ($ query );
- }
- Echo "\ r \ nUSE PDO:". (microtime (true)-$ start_time );
- /**
- USE MySQL extension: 95.233189106 s
- Use pdo: 99.1193888187 s
- There is almost no difference in connecting to MySQL. The performance loss of PDO is negligible.
- However, many operations are not available in the MySQL extension Library:
- 1: PDO implements the underlying unified interface library operation interface
- 2: PDO supports more advanced DB feature operations, such as stored procedure scheduling, which is not supported by the mysql native database.
- 3: PDO is the official PECL library of PHP, and its compatibility and stability must be higher than MySQL Extension. you can directly upgrade it using the pecl upgrade pdo command.
- PHP6 uses PDO for database connections by default, and MySQL Extension serves as an aid.
- Therefore, in daily projects, if the environment permits, try to use PDO for MySQL database operations.
- ?>
|