Three major APIs commonly used in php link-MySQL database server: mysql, mysqli, and pdo

Source: Internet
Author: User
Tags mysql connect
Three major APIs commonly used in php link-MySQL database server: mysql, mysqli, and pdo
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)

  1. /**
  2. After reading a lot of related database connections and operations
  3. A self-summary of database connection in php
  4. ------------ Hope to be helpful
  5. */
  6. // 1 connection type
  7. /**
  8. Three major APIs commonly used in php link-MySQL database server: mysql, mysqli, and pdo
  9. /******** Basic information *********/
  10. A. API -------------- Application Programming Interface
  11. The Application Interface (abbreviated as Application Programming Interface) defines classes, methods, functions,
  12. Variables, and so on. In the PHP application
  13. APIs required for interacting with databases are usually exposed through PHP extensions (called by terminal php programmers ).
  14. APIs can be process-oriented or object-oriented. For process-oriented APIs, we call functions to complete
  15. For object-oriented APIs, we instantiate classes and call methods on the objects obtained after instantiation.
  16. For these two interfaces, the latter is usually the first choice, because it is more modern and brings us good
  17. Code structure.
  18. B. connector ----- "a piece of software code that allows your application to connect to the MySQL database server ".
  19. When your PHP application needs to interact with a database server, you need to write php code to complete "connect data
  20. Database server, query database, and other database-related functions. Your PHP application will use
  21. Software for these APIs, or use intermediate libraries as needed to process the connection between your applications and database servers
  22. .
  23. C. Driver
  24. Software code used to interact with a specific type of database server. The driver may call some libraries,
  25. For example, the MySQL client library or the MySQL Native driver library. These libraries are used for MySQL database services.
  26. The underlying protocol for interaction.
  27. The connector and driver terms may not be differentiated.
  28. In MySQL documentation, the term "driver" is used as a connector package.
  29. Provides software code for specific database sections.
  30. D. What is expansion?
  31. You will also find many other extensions in the PHP document. PHP code is a core and some optional extensions constitute the core functions.
  32. MySQL-related extensions of PHP, such as mysqli and mysql, are implemented based on the PHP extension framework.
  33. A typical function of extension is to expose an API to PHP programmers and allow them to extend their functions.
  34. Of course, some extensions developed based on the PHP Extension Framework do not expose API interfaces to PHP programmers.
  35. 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.
  36. The term API and the extension describe different things, because the extension may not need to expose an API interface to the programmer.
  37. *******/
  38. Main APIs provided in PHP for MySQL:
  39. ■ MySQL extension of PHP
  40. (Advantages and disadvantages)
  41. Design and Development early extensions that allow PHP applications to interact with MySQL databases. Mysql extension provides a process-oriented interface,
  42. 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
  43. The database server interacts with each other, but does not support some features provided by the MySQL server later.
  44. ]
  45. ■ Mysqli extension of PHP
  46. 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.
  47. MySQL I extensions are included in PHP 5 and later versions.
  48. The mysqli extension has a series of advantages, which are mainly improved compared with the mysql extension:
  49. ■ Object-oriented interface
  50. ■ Supported prepared statements)
  51. ■ Multi-statement execution support
  52. ■ Transaction support
  53. ■ Enhanced debugging capability
  54. ■ Embedded service support
  55. ■ PHP Data object (PDO)
  56. PHP Data objects are a database abstraction layer specification in PHP applications. PDO provides a unified API
  57. So that your PHP application does not care about the type of database server system to be connected. That is to say,
  58. If you use the pdo api, you can seamlessly switch the database server whenever necessary.
  59. ***********/
  60. PHP-MySQL is the original Extension for PHP to operate MySQL databases. I of PHP-MySQLi represents Improvement,
  61. More advanced functions are provided. Extension also increases security.
  62. The PDO (PHP Data Object) provides an action Layer to operate databases.
  63. Detailed source reference: http://www.jb51.net/article/28103.htm
  64. 1. mysql and mysqli
  65. Mysqli is a new function library provided by php5. (I) indicates improvement, and the execution speed is faster. of course, it is more secure.
  66. Detailed source reference: http://www.jb51.net/article/28103.htm
  67. Mysql is a non-persistent connection function, while mysqli is a permanent connection function. That is to say
  68. Mysql opens a connection process at each link, and mysqli runs mysqli multiple times and uses the same connection process,
  69. This reduces the server overhead.
  70. Some friends always report new mysqli ('localhost', usenamer ', 'password', 'databasename') during programming.
  71. Error, Fatal error: Class 'mysqli' not found in d :\...
  72. Isn't the mysqli class included in php?
  73. 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.
  74. I. Mysqli. dll is an object-based or process-based database that is easy to use. Here are a few examples
  75. Detailed source reference: http://www.jb51.net/article/28103.htm
  76. Do more, do less talk:
  77. */
  78. Mysql_connect ($ db_host, $ db_user, $ db_password );
  79. Mysql_select_db ($ dn_name );
  80. $ Result = mysql_query ("SELECT 'name' FROM 'users' WHERE 'location' = '$ location '");
  81. While ($ row = mysql_fetch_array ($ result, MYSQL_ASSOC ))
  82. {
  83. Echo $ row ['name'];
  84. }
  85. Mysql_free_result ($ result );
  86. /**
  87. In fact, there are some knowledge behind it...
  88. This method cannot Bind Column. in the previous SQL statement, $ location is prone to SQL Injection.
  89. Later, I developed mysql_escape_string () (Note: Use it out after 5.3.0) and mysql_real_escape_string ()
  90. 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...
  91. Detailed source reference: http://www.jb51.net/article/28103.htm
  92. */
  93. $ Query = sprintf ("SELECT * FROM users WHERE user = '% s' AND password =' % s '",
  94. Mysql_real_escape_string ($ user ),
  95. Mysql_real_escape_string ($ password ));
  96. Mysql_query ($ query );
  97. /**
  98. 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,
  99. The Object oriented style and Procedural style are also provided.
  100. (The above PHP-MySQL example) two types of writing... And so on.
  101. Detailed source reference: http://www.jb51.net/article/28103.htm
  102. */
  103. $ Mysqli = new mysqli ($ db_host, $ db_user, $ db_password, $ db_name );
  104. $ SQL = "INSERT INTO 'Users' (id, name, gender, location) VALUES (?, ?, ?, ?) ";
  105. $ Stmt = $ mysqli-> prepare ($ SQL );
  106. $ Stmt-> bind_param ('dss', $ source_id, $ source_name, $ source_gender, $ source_location );
  107. $ Stmt-> execute ();
  108. $ Stmt-> bind_result ($ id, $ name, $ gender, $ location );
  109. While ($ stmt-> fetch ())
  110. {
  111. Echo $ id. $ name. $ gender. $ location;
  112. }
  113. $ Stmt-> close ();
  114. $ Mysqli-> close ();
  115. /**
  116. However, some shortcomings have been found here, such as the Bind Result. this is a bit redundant, but it doesn't matter,
  117. Because the biggest problem is that this is not an abstract action method, it is a painful start when the backend changes the database...
  118. So PDO appeared.
  119. Detailed source reference: http://www.jb51.net/article/28103.htm
  120. */
  121. // 2. PDO and mysql
  122. /*
  123. PDO is supported only after PHP5.1. it adopts a consistent interface for accessing the database. However, many open-source programs in China are
  124. 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?
  125. I have asked a few friends why I use PDO. The answer is "fast". will PDO connect to the database quickly? Why use PDO?
  126. 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.
  127. */
  128. $ Link = mysql_connect ("localhost", "root", "root") or die ('MySQL connect error ');
  129. $ Num = 100000;
  130. $ Dsn = "mysql: host = 127.0.0.1; dbname = performace_test ";
  131. $ Db = new PDO ($ dsn, 'root', 'root', array (PDO: ATTR_PERSISTENT => true ));
  132. Mysql_query ('truncate TABLE 'into Mace _ Test'. 'myquery', $ link); // TRUNCATE Table
  133. $ 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 ')";
  134. $ Start_time = microtime (true );
  135. For ($ I = 0; $ I <$ num; $ I ++)
  136. {
  137. Mysql_query ($ query, $ link );
  138. }
  139. Echo "USE MySQL extension:". (microtime (true)-$ start_time );
  140. Mysql_query ('truncate TABLE 'into Mace _ Test'. 'myquery', $ link); // TRUNCATE Table
  141. $ Start_time = microtime (true );
  142. For ($ I = 0; $ I <$ num; $ I ++)
  143. {
  144. $ Db-> exec ($ query );
  145. }
  146. Echo "\ r \ nUSE PDO:". (microtime (true)-$ start_time );
  147. /**
  148. USE MySQL extension: 95.233189106 s
  149. Use pdo: 99.1193888187 s
  150. There is almost no difference in connecting to MySQL. The performance loss of PDO is negligible.
  151. However, many operations are not available in the MySQL extension Library:
  152. 1: PDO implements the underlying unified interface library operation interface
  153. 2: PDO supports more advanced DB feature operations, such as stored procedure scheduling, which is not supported by the mysql native database.
  154. 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.
  155. PHP6 uses PDO for database connections by default, and MySQL Extension serves as an aid.
  156. Therefore, in daily projects, if the environment permits, try to use PDO for MySQL database operations.
  157. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.