-
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)
- /**
- See a lot of related database connections and operations after the
- A self-summary of database connections in PHP
- ------------hope to be of some benefit
- */
- 1 Connection Type
- /**
- PHP three major api:mysql,mysqli,pdo differences and links used in Link-mysql database server
- /******** Basic related Information *********/
- A.API----------------Application Programming Interface
- The Application Interface (abbreviated application programming Interface), defines classes, methods, functions,
- Variables and so on everything in your application that needs to be called in order to complete a particular task. The PHP application requires
- The APIs needed to interact with the database are usually exposed via PHP extensions (called to the terminal PHP programmer).
- The API can be either process-oriented or object-oriented. For process-oriented APIs, we use the call function to finish
- 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.
- For both interfaces, the latter is usually preferred because it is more modern and brings us a good
- Code structure.
- B.connector-----"A piece of software code that allows your app to connect to a MySQL database server."
- When your PHP application needs to interact with a database server, you need to write the PHP code to complete the "Connect data
- Library server, query database, and other database-related functions. Your PHP application will use the
- Software for these APIs, or use some intermediate libraries when needed to process your application and the database server
- The interaction.
- C. Drive
- Software code used to interact with a particular type of database server. The driver may call some libraries,
- such as MySQL client library or MySQL native driver library. These libraries are implemented for and MySQL database services
- The underlying protocol for interacting with the device.
- It is possible that you will not differentiate between the use of connectors and drivers for both terms.
- The term "driver" is used as a connector package in MySQL related documentation
- Provides software code for a specific database section in the.
- D. What is an extension?
- 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.
- MySQL-related extensions for PHP, such as Mysqli,mysql, are implemented based on the PHP extension framework.
- 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.
- 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.
- 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.
- The term API and extension describe not the same thing, because the extension may not need to expose an API interface to programmers.
- /********* Key *******/
- The main APIs available in PHP for MySQL are:
- MySQL Extension for PHP
- Advantages and Disadvantages
- 【
- Design and development allows an early extension of PHP applications to interact with the MySQL database. The MySQL extension provides a process-oriented interface,
- 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
- The library server, but does not support some of the features provided by the post-MySQL server.
- 】
- PHP mysqli Extensions
- Mysqli extensions, which we sometimes call MySQL enhanced extensions, can be used for new advanced features in MySQL4.1.3 or later versions.
- The mysqli extension is included in PHP 5 and later.
- The mysqli extension has a number of advantages, as compared to the MySQL extension, the main improvements are:
- Object-oriented interface
- Prepared statement support (see MySQL related documentation for prepare)
- Multi-statement execution support
- Transaction support
- Enhanced debugging capabilities
- Embedded service Support
- PHP Data Objects (PDO)
- PHP Data Objects are a Database abstraction layer specification in PHP applications. PDO provides a unified API interface that can
- So that your PHP application does not care about the type of database server system you want to connect to. Other words
- If you use the PDO API, you can seamlessly switch the database server whenever you need it
- /******* contrast ***********/
- Php-mysql is the most primitive Extension of PHP operation MySQL Database, php-mysqli I represents improvement,
- In addition to the relative advanced function, in terms of Extension, it also adds security.
- The PDO (PHP Data Object) provides a abstraction Layer to manipulate the database
- For more information, refer to: http://www.jb51.net/article/28103.htm
- 1.mysql and Mysqli
- Mysqli is a new library of functions provided by PHP5, and (i) represents an improvement that executes faster. Of course it's safer.
- For more information, refer to: http://www.jb51.net/article/28103.htm
- MySQL is a non-holding connection function and Mysqli is a permanent connection function. Other words
- MySQL each link opens a connected process and mysqli runs multiple times mysqli will use the same connection process,
- Thus reducing the overhead of the server
- Some friends use new mysqli (' localhost ', usenamer ', ' Password ', ' DatabaseName ') when programming, and always quote
- Wrong, Fatal Error:class ' mysqli ' not found in d:\ ...
- Mysqli class is not PHP comes with it?
- Not by default, win under to change php.ini, remove Php_mysqli.dll before, Linux to put Mysqli in.
- 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
- For more information, refer to: http://www.jb51.net/article/28103.htm
- Do more things and talk less:
- */
- Mysql_connect ($db _host, $db _user, $db _password);
- mysql_select_db ($dn _name);
- $result = mysql_query ("Select ' Name ' from ' The Users ' WHERE ' location ' = ' $location '");
- while ($row = Mysql_fetch_array ($result, MYSQL_ASSOC))
- {
- echo $row [' name '];
- }
- Mysql_free_result ($result);
- /**
- In fact, some knowledge behind ...
- This method can not Bind Column, the previous example of SQL narration, $location Place is easy to be SQL injection.
- Subsequently developed mysql_escape_string (note: 5.3.0 after the deprecation) and mysql_real_escape_string ()
- 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.
- For more information, refer to: 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);
- /**
- There has been a lot of progress in php-mysqli, except through Bind Column to solve the above problems, but also Transaction, Multi Query,
- It also provides an Object oriented style (the php-mysqli example below) and procedural style
- (Php-mysql examples above) two ways to write ... Wait a minute.
- For more information, refer to: 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 (' dsss ', $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 ();
- /**
- But see here and found some shortcomings, such as Bind Result, this is a little bit more, but it does not matter,
- 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 ...
- And then PDO appeared.
- For more information, refer to: http://www.jb51.net/article/28103.htm
- */
- 2.PDO and MySQL
- /*
- 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
- 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?
- Asked a few friends why with PDO, the answer is "fast", PDO connection database will be fast? Why use PDO?
- 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.
- */
- $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 ' performace_test ', ' myquery ', $link); Truncate Table
- $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 ');
- $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 ' performace_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.233189106s
- Use pdo:99.1193888187s
- There's almost no difference in linking MySQL. The performance loss of PDO can be completely negligible.
- But there are a lot of things that the MySQL extension library does not have:
- 1:pdo real-to-bottom implementation of the unified Interface number Library operation interface
- 2:PDO supports more advanced DB feature operations, such as the scheduling of stored procedures, which are not supported by MySQL native libraries.
- 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
- PHP6 The default is to use PDO for database links, MySQL extension will be used as a secondary.
- So in our daily projects, if the environment permits, use PDO as much as possible to perform MySQL database operations.
- ?>
Copy Code |