PHP three major api:mysql,mysqli,pdo differences and links used in Link-mysql database server

Source: Internet
Author: User
Tags dsn mysql connect
PHP three main api:mysql,mysqli,pdo differences and connections when connecting to MySQL database server
is also a commonly used extension, which of their performance is excellent?

It's all good, but it's better in comparison (I like PDO)
  1. /**
  2. See a lot of related database connections and operations after the
  3. A self-summary of database connections in PHP
  4. ------------hope to be of some benefit
  5. */
  6. 1 Connection Type
  7. /**
  8. PHP three major api:mysql,mysqli,pdo differences and links used in Link-mysql database server
  9. /******** Basic related Information *********/
  10. A.API----------------Application Programming Interface
  11. The Application Interface (abbreviated application programming Interface), defines classes, methods, functions,
  12. Variables and so on everything in your application that needs to be called in order to complete a particular task. The PHP application requires
  13. The APIs needed to interact with the database are usually exposed via PHP extensions (called to the terminal PHP programmer).
  14. The API can be either process-oriented or object-oriented. For process-oriented APIs, we use the call function to finish
  15. into the task, and for the object-oriented API, we instantiate the class and invoke the method on the object that was obtained after the instantiation.
  16. For both interfaces, the latter is usually preferred because it is more modern and brings us a good
  17. Code structure.
  18. B.connector-----"A piece of software code that allows your app to connect to a MySQL database server."
  19. When your PHP application needs to interact with a database server, you need to write the PHP code to complete the "Connect data
  20. Library server, query database, and other database-related functions. Your PHP application will use the
  21. Software for these APIs, or use some intermediate libraries when needed to process your application and the database server
  22. The interaction.
  23. C. Drive
  24. Software code used to interact with a particular type of database server. The driver may call some libraries,
  25. such as MySQL client library or MySQL native driver library. These libraries are implemented for and MySQL database services
  26. The underlying protocol for interacting with the device.
  27. It is possible that you will not differentiate between the use of connectors and drivers for both terms.
  28. The term "driver" is used as a connector package in MySQL related documentation
  29. Provides software code for a specific database section in the.
  30. D. What is an extension?
  31. You will also find many other extensions in the PHP documentation. PHP code is made up of a core, and some optional extensions make up the core functionality.
  32. MySQL-related extensions for PHP, such as Mysqli,mysql, are implemented based on the PHP extension framework.
  33. An extension of a typical function is to expose an API to a PHP programmer, allowing the extension of its own functionality to be used by programmers.
  34. Of course, there are also a subset of extensions that are developed based on the PHP extension framework that will not expose the API interface to PHP programmers.
  35. For example, the PDO MySQL driver extension does not expose the API interface to a PHP programmer, but it provides an interface to the PDO layer on its upper level.
  36. The term API and extension describe not the same thing, because the extension may not need to expose an API interface to programmers.
  37. /********* Key *******/
  38. The main APIs available in PHP for MySQL are:
  39. MySQL Extension for PHP
  40. Advantages and Disadvantages
  41. Design and development allows an early extension of PHP applications to interact with the MySQL database. The MySQL extension provides a process-oriented interface,
  42. and is designed for MySQL4.1.3 or earlier versions. Therefore, although this extension can be compared with the MySQL4.1.3 or updated number of
  43. The library server, but does not support some of the features provided by the post-MySQL server.
  44. PHP mysqli Extensions
  45. Mysqli extensions, which we sometimes call MySQL enhanced extensions, can be used for new advanced features in MySQL4.1.3 or later versions.
  46. The mysqli extension is included in PHP 5 and later.
  47. The mysqli extension has a number of advantages, as compared to the MySQL extension, the main improvements are:
  48. Object-oriented interface
  49. Prepared statement support (see MySQL related documentation for prepare)
  50. Multi-statement execution support
  51. Transaction support
  52. Enhanced debugging capabilities
  53. Embedded service Support
  54. PHP Data Objects (PDO)
  55. PHP Data Objects are a Database abstraction layer specification in PHP applications. PDO provides a unified API interface that can
  56. So that your PHP application does not care about the type of database server system you want to connect to. Other words
  57. If you use the PDO API, you can seamlessly switch the database server whenever you need it
  58. /******* contrast ***********/
  59. Php-mysql is the most primitive Extension of PHP operation MySQL Database, php-mysqli I represents improvement,
  60. In addition to the relative advanced function, in terms of Extension, it also adds security.
  61. The PDO (PHP Data Object) provides a abstraction Layer to manipulate the database
  62. For more information, refer to: http://www.jb51.net/article/28103.htm
  63. 1.mysql and Mysqli
  64. Mysqli is a new library of functions provided by PHP5, and (i) represents an improvement that executes faster. Of course it's safer.
  65. For more information, refer to: http://www.jb51.net/article/28103.htm
  66. MySQL is a non-holding connection function and Mysqli is a permanent connection function. Other words
  67. MySQL each link opens a connected process and mysqli runs multiple times mysqli will use the same connection process,
  68. Thus reducing the overhead of the server
  69. Some friends use new mysqli (' localhost ', usenamer ', ' Password ', ' DatabaseName ') when programming, and always quote
  70. Wrong, Fatal Error:class ' mysqli ' not found in d:\ ...
  71. Mysqli class is not PHP comes with it?
  72. Not by default, win under to change php.ini, remove Php_mysqli.dll before, Linux to put Mysqli in.
  73. One: Mysqli.dll is a database that allows the operation of objects in a way or process, and it is easy to use. Here are a few often
  74. For more information, refer to: http://www.jb51.net/article/28103.htm
  75. Do more things and talk less:
  76. */
  77. Mysql_connect ($db _host, $db _user, $db _password);
  78. mysql_select_db ($dn _name);
  79. $result = mysql_query ("Select ' Name ' from ' The Users ' WHERE ' location ' = ' $location '");
  80. while ($row = Mysql_fetch_array ($result, MYSQL_ASSOC))
  81. {
  82. echo $row [' name '];
  83. }
  84. Mysql_free_result ($result);
  85. /**
  86. In fact, some knowledge behind ...
  87. This method can not Bind Column, the previous example of SQL narration, $location Place is easy to be SQL injection.
  88. Subsequently developed mysql_escape_string (note: 5.3.0 after the deprecation) and mysql_real_escape_string ()
  89. To solve this problem, but with this, the whole narrative becomes complex and ugly, and if there are more columns, you can see what the situation is.
  90. For more information, refer to: http://www.jb51.net/article/28103.htm
  91. */
  92. $query = sprintf ("SELECT * from Users WHERE user= '%s ' and password= '%s '",
  93. Mysql_real_escape_string ($user),
  94. Mysql_real_escape_string ($password));
  95. mysql_query ($query);
  96. /**
  97. There has been a lot of progress in php-mysqli, except through Bind Column to solve the above problems, but also Transaction, Multi Query,
  98. It also provides an Object oriented style (the php-mysqli example below) and procedural style
  99. (Php-mysql examples above) two ways to write ... Wait a minute.
  100. For more information, refer to: http://www.jb51.net/article/28103.htm
  101. */
  102. $mysqli = new Mysqli ($db _host, $db _user, $db _password, $db _name);
  103. $sql = "INSERT into ' users ' (ID, name, gender, location) VALUES (?,?,?,?)";
  104. $stmt = $mysqli->prepare ($sql);
  105. $stmt->bind_param (' dsss ', $source _id, $source _name, $source _gender, $source _location);
  106. $stmt->execute ();
  107. $stmt->bind_result ($id, $name, $gender, $location);
  108. while ($stmt->fetch ())
  109. {
  110. Echo $id. $name. $gender. $location;
  111. }
  112. $stmt->close ();
  113. $mysqli->close ();
  114. /**
  115. But see here and found some shortcomings, such as Bind Result, this is a little bit more, but it does not matter,
  116. Because the biggest problem is that it's not an abstract (abstraction) approach, so when the backend changes the database, it's the beginning of the pain ...
  117. And then PDO appeared.
  118. For more information, refer to: http://www.jb51.net/article/28103.htm
  119. */
  120. 2.PDO and MySQL
  121. /*
  122. PDO was supported after PHP5.1, and he used a consistent interface for accessing the database. But many of the country's open source programs are
  123. Use the function provided by MySQL extension to connect to the database for querying. PDO is powerful why is the domestic mature PHP system not used?
  124. Asked a few friends why with PDO, the answer is "fast", PDO connection database will be fast? Why use PDO?
  125. What's the difference between the two of them? First of all, it is more concerned with performance issues. wrote 1 script tests to insert 1 million data into MySQL.
  126. */
  127. $link = mysql_connect ("localhost", "root", "root") or Die (' MySQL connect error ');
  128. $num = 100000;
  129. $DSN = "Mysql:host=127.0.0.1;dbname=performace_test";
  130. $db = new PDO ($dsn, ' root ', ' root ', array (pdo::attr_persistent = true));
  131. mysql_query (' TRUNCATE TABLE ' performace_test ', ' myquery ', $link); Truncate Table
  132. $query = "INSERT into ' performace_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′, ' 298′, ' 65′, ' 100′, ' 0.125′, ' smt000080′, ' health, ', ' health ', ' images/ 201004/thumb_img/80_thumb_g_1272071721054.jpg ', ' 1′, ' 0′, ' 0′,null,null,null, ' 0′, ' 2980.00′, ' 0.00′, ' 1.250000′, ' 1′, ' 1271612064′, ' 0′, ' 1297624384′, ' 1293649512083026412.jpg ');
  133. $start _time = Microtime (true);
  134. for ($i =0; $i < $num; $i + +)
  135. {
  136. mysql_query ($query, $link);
  137. }
  138. echo "Use MySQL extension:". (Microtime (True)-$start _time);
  139. mysql_query (' TRUNCATE TABLE ' performace_test ', ' myquery ', $link); Truncate Table
  140. $start _time = Microtime (true);
  141. for ($i =0; $i < $num; $i + +)
  142. {
  143. $db->exec ($query);
  144. }
  145. echo "\r\nuse PDO:". (Microtime (True)-$start _time);
  146. /**
  147. Use MySQL extension:95.233189106s
  148. Use pdo:99.1193888187s
  149. There's almost no difference in linking MySQL. The performance loss of PDO can be completely negligible.
  150. But there are a lot of things that the MySQL extension library does not have:
  151. 1:pdo real-to-bottom implementation of the unified Interface number Library operation interface
  152. 2:PDO supports more advanced DB feature operations, such as the scheduling of stored procedures, which are not supported by MySQL native libraries.
  153. 3:pdo is the official PHP pecl library, compatibility stability must be higher than the MySQL Extension, you can directly use the PECL Upgrade PDO command upgrade
  154. PHP6 The default is to use PDO for database links, MySQL extension will be used as a secondary.
  155. So in our daily projects, if the environment permits, use PDO as much as possible to perform MySQL database operations.
  156. ?>
Copy Code
  • 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.