This article introduces, PHP in about mysqli and MySQL class some differences, have the need for friends, can make a reference oh. One, Php-mysql is PHP operation MySQL Database The most primitive Extension, php-mysqli I for improvement, mention the relative advanced function. The PDO (PHP Data Object) provides a abstraction Layer to manipulate the database. Example:
This method can not Bind Column, the previous example of SQL narration, $location Place is easy to be SQL injection. Later, mysql_escape_string () (note: deprecated after 5.3.0) and mysql_real_escape_string () were developed to resolve the issue. Example:
A lot of progress has been made in php-mysqli, except through Bind Column to solve the above problem, but also Transaction, Multi Query, and also provides an Object oriented style (below this paragraph php-mysq Li example) and procedural style (above Php-mysql example) two ways to write. Example:
Disadvantages, such as Bind Result, this is a little extra, but it doesn't really matter, because the biggest problem is that it's not an abstract (abstraction) approach. So PDO appeared (note: Currently, for Ubuntu and Debian, PDO does not have a direct kit to install, but must be installed via 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 (Stab Le) PDO driver for IBM informix Informix databases Pdo_mysql 1.0.2 (Stable) MYSQL driver for PDO Pdo_oci 1.0 (Stable) Orac Le call Interface driver-PDO Pdo_odbc 1.0.1 (Stable) ODBC v3 Interface driver for PDO Pdo_pgsql 1.0.2 (Stable) Postgre SQL driver for PDO Pdo_sqlite 1.0.1 (Stable) SQLITE v3 Interface driver for PDO Pdo_user 0.3.0 (Beta) userspace driver for PdoWhen installed through the PECL installation, you can operate the database in the following ways:
Benefits of PDO: 1,pdo connects to a database by Connection String to determine which database to connect to. 2,pdo can be pdo::setattribute to determine the connection settings, such as persistent Connection, return the wrong Way (Exception, e_warning, NULL). Even the case of the callback field name ... Wait a minute. 2,pdo supports the function of BIND column, in addition to the basic Prepare, Execute, can bind a single field, and specify the field type. 4,pdo is the abstraction Layer, so even if the replacement of storage media, the need to spend more effort than is the least. Individuals prefer to use DBI to connect to a database, such as ActiveRecord and Propel ORM (object-relational Mapping). For example, take ActiveRecord as an example, if you want to implement such a SQL narrative ... INSERT into ' users ' (ID, name, gender, location) VALUES (1, ' Roga ', ' Male ', ' TPE ') PDO operation mode:
In the case of ActiveRecord, it is:
MySQL is a non-holding connection function and Mysqli is a permanent connection function. This means that MySQL each time the link opens a connected process and mysqli multiple runs mysqli will use the same connection process, thereby reducing the server overhead. Some friends in programming, use new mysqli (' localhost ', usenamer ', ' Password ', ' databasename '), always error, Fatal error:class ' mysqli ' not found I N d:\ ... mysqli class is not PHP's own? 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. Several common operations and Mysql.dll make a comparison. 1,
Mysqli also have a procedural approach, but start with the mysqli prefix, the others are similar. If Mysqli is manipulated in a procedural way, some functions must specify a resource, such as mysqli_query (resource ID, SQL statement), and the resource identification parameter is placed in front, while the mysql_query (SQL statement, ' optional ') resource identifier is placed in the back , and can not be specified, it is the last open connection or resource by default. 2,mysqli.dll (object mode):
Two, Mysql_fetch_row (), mysql_fetch_array () These functions, the return is an array, the difference is that the first function returns an array that contains only the value, only $row[0], $row [1], so that the array subscript to read the data, The array returned by Mysql_fetch_array () contains both the first and the form of a key-value pair, so that the data can be read (if the database field is USERNAME,PASSWD): $row [' username '], $row [' passwd '] Furthermore, if you use ($row as $kay + $value), you also get the field name of the database directly. More important is that mysqli is a new library of functions provided by PHP5, (i) for improvement, and for faster execution. The above is the PHP programming in MySQL and mysqli the difference between, I hope that we have some help. |