This article introduces some differences between mysqli and mysql in php. if you have any need, you can make a reference. I. PHP-MySQL is the original Extension for PHP to operate MySQL databases. I of PHP-MySQLi represents Improvement and provides more advanced functions. PDO (PHP Data Object) provides an action Layer to operate databases. Example:
This method cannot Bind Column. in the previous SQL statement, $ location is prone to SQL Injection. As a result, mysql_escape_string () (remarks: discarded after 5.3.0) and mysql_real_escape_string () were developed to solve this problem. Example:
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 provided at the same time. Example:
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(); ?> Disadvantages: for example, if Bind Result is obtained, this is a bit redundant, but it doesn't matter, because the biggest problem is that this is not an abstract action method. As a result, PDO appears (note: for Ubuntu and Debian, PDO does not have a direct suite to install, but must be installed through PECL ). Example: Roga @ carlisten-lx :~ $ Pecl search pdo Package Stable/(Latest) Local PDO 1.0.3 (stable) PHP Data Objects Interface. PDO_4D 0.3 (beta) PDO driver for 4D-SQL database PDO_DBLIB 1.0 (stable) FreeTDS/Sybase/MSSQL driver for PDO PDO_FIREBIRD 0.2 (beta) Firebird/InterBase 6 driver for PDO PDO_IBM 1.3.2 (stable) PDO driver for IBM databases PDO_INFORMIX 1.2.6 (stable) PDO driver for IBM Informix INFORMIX databases PDO_MYSQL 1.0.2 (stable) MySQL driver for PDO PDO_OCI 1.0 (stable) oracle Call Interface driver for PDO PDO_ODBC 1.0.1 (stable) ODBC v3 Interface driver for PDO PDO_PGSQL 1.0.2 (stable) PostgreSQL driver for PDO PDO_SQLITE 1.0.1 (stable) SQLite v3 Interface driver for PDO pdo_user 0.3.0 (beta) Userspace driver for PDOAfter the installation is completed through PECL, you can operate the database in the following ways:
prepare($sql); $sth->execute(array($location, $name)); $result = $sth->fetch(PDO::FETCH_OBJ); echo $result->name . $result->location; $dbh = NULL; ?> Benefits of pdo: 1. when a PDO connects to a database, the Connection String is used to determine the database to be connected. 2. PDO can determine the Connection settings through PDO: setAttribute, such as Persistent Connection and return error (Exception, E_WARNING, NULL ). Even the case of the return Field name... And so on. 2. PDO supports the Bind Column function. in addition to basic Prepare and Execute, PDO can also specify a single Column of Bind and a Column type. 4. PDO is a physical action Layer. Therefore, it takes the least effort to replace the storage media. I personally prefer to use DBI to connect to databases, such as ActiveRecord and Propel ORM (Object-Relational Mapping ). For example, taking ActiveRecord as an example, if you want to implement such an SQL statement... Insert into 'Users' (id, name, gender, location) VALUES (1, 'roga ', 'male', 'tpe') pdo operation method:
prepare($sql); $sth->execute(array(1, 'roga', 'male', 'tpe')); ?> For ActiveRecord, it is:
id = 1; $user->name = 'roga'; $user->gender = 'male'; $user->location = 'tpe'; $user->save(); ?> Mysql is a non-persistent connection function, while mysqli is a permanent connection function. That is to say, mysql will open a connection process each time it connects, and mysqli will use the same connection process to run mysqli multiple times, thus reducing the overhead of the server. Some friends use new mysqli ('localhost', usenamer ', 'password', 'databasename') during programming; always report an 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. Several common operations are compared with mysql. dll. 1,
Mysqli also has a program method, but starts to use the mysqli prefix, and the others are similar. If mysqli operates in a procedural manner, some functions must specify resources, for example, mysqli_query (resource ID, SQL statement), and the parameters of the resource ID are placed in the front, the resource ID of mysql_query (SQL statement, 'option') is placed behind and can be left unspecified. it is the last opened connection or resource by default. 2, mysqli. dll (object method ):
Select_db ('data _ base') $ result = $ conn-> query ('select * from data_base '); $ row = $ result-> fetch_row (); // Obtain a row of data echo row [0]; // output the value of the first field 2. the two functions mysql_fetch_row () and mysql_fetch_array () return an array. The difference is that the array returned by the first function only contains values and can only be $ row [0]. $ row [1], which reads data by array subscript. the array returned by mysql_fetch_array () contains both the first and key-value pairs. you can read data in this way, (assume that the database field is username, passwd): $ row ['username'], $ row ['passwd'] and, if you use ($ row as $ kay => $ value) for operations, you can directly obtain the database field name. What's more, mysqli is a new function library provided by php5. (I) indicates improvement and its execution speed is faster. The above is the difference between mysql and mysqli in php programming. I hope to help you. |