The POD extension was added in PHP5. This extension provides PHP's built-in class PDO to access the database. Different databases use the same method name to solve the problem of inconsistent database connections.
The goal of PDO is
to provide a lightweight, clear, and convenient API
Unify features common to various RDBMS libraries without excluding more advanced features.
Provides an optional greater degree of abstraction/compatibility via PHP scripts.
Features of PDO:
Performance. PDO learned from the beginning about the successes and failures of scaling existing databases. Because PDO's code is brand new, we have the opportunity to redesign performance from the ground up to take advantage of PHP 5's latest features.
ability. PDO is designed to provide common database functionality as a foundation while providing easy access to the unique features of an RDBMS.
Simple. PDO is designed to make working with databases easy for you. The API doesn't force its way into your code and makes it clear what each function call does.
Extensible at runtime. The PDO extension is modular, enabling you to load drivers for your database backend at runtime without having to recompile or reinstall the entire PHP program. For example, the PDO_OCI extension will replace the PDO extension to implement the Oracle database API. There are also drivers for MySQL, PostgreSQL, ODBC, and Firebird, with more in development. [separator]
Install PDO
What I have here is a PDO extension for development under WINDOWS. If you want to install and configure it under Linux, please look elsewhere.
Version requirements: php5.1 and later versions are already included in the package; php5.0.x needs to be downloaded from pecl.php.net and placed in your extension library, which is the ext folder of the folder where PHP is located. ;The manual says that versions prior to 5.0 cannot run PDO extensions. Configuration:
Modify your php.ini configuration file so that it supports pdo. (If you don’t understand php.ini, figure it out first. You need to modify the php.ini displayed when calling your phpinfo() function.) Change extension=php_pdo Remove the semicolon in front of .dll. The semicolon is the php configuration file comment symbol. This extension is necessary. There are more
;extension=php_pdo.dll
;extension=php_pdo_firebird.dll
;extension=php_pdo_informix.dll
;extension=php_pdo_mssql.dll
;extension=php_pdo_mysql.dll
;extension=php_pdo_oci.dll
;extension=php_pdo_oci8.dll
;extension=php_pdo_odbc.dll
;extension=php_pdo_pgsql.dll
;extension=php_pdo_sqlite.dll
The database corresponding to each extension is:
Driver name Supported databases
PDO_DBLIB FreeTDS/Microsoft SQL Server/Sybase
PDO_FIREBIRD Firebird/Interbase 6
PDO_INFORMIX IBM Informix Dynamic Server
PDO_MYSQL MySQL 3.x/4.x
PDO_OCI Oracle Call Interface
PDO_ODBC ODBC v3 (IBM DB2, unixODBC and win32 ODBC)
PDO_PGSQL PostgreSQL
PDO_SQLITE SQLite 3 and SQLite 2
Which database you want to use, just remove the comment symbol ";" before the corresponding extension.
Using PDO
I assume here that you have installed mysql. If not, please find a way to install it first. Mine is mysql5.0.22, and others who use MySQL 4.0.26 can also use it.
Database connection:
We use the following example to analyze the PDO connection database,
<?php
$dbms='mysql'; //Database type oracle uses ODI. For developers, using different databases, as long as you change this, you don't need to remember so many functions.
$host='localhost'; //Database host name
$dbName='test'; //Database used
$user='root'; //Database connection user name
$pass=''; //Corresponding password
$dsn="$dbms:host=$host;dbname=$dbName";
try {
$dbh = new PDO($dsn, $user, $pass); //Initializing a PDO object means creating the database connection object $dbh
echo "Connection successful<br/>";
/*You can also perform a search operation
foreach ($dbh->query('Select * from FOO') as $row) {
print_r($row); //You can use echo($GLOBAL); to see these values
}
*/
$dbh = null;
} catch (PDOException $e) {
die ("Error!: " . $e->getMessage() . "<br/>");
}
//By default, this is not a long connection. If you need a long connection to the database, you need to add a parameter at the end: array(PDO::ATTR_PERSISTENT => true). It becomes like this:
$db = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true));
?>
Database query:
We have already performed a query above, and we can also use the following query:
<?php
$db->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER); //Set attributes
$rs = $db->query("Select * FROM foo");
$rs->setFetchMode(PDO::FETCH_ASSOC);
$result_arr = $rs->fetchAll();
print_r($result_arr);
?>
Because the setAttribute() method is used in the above, the two parameters are put in to force the field name to uppercase. The following lists the parameters of PDO::setAttribute(): PDO::ATTR_CASE: Forces column names to be in a format, as detailed below (second parameter):
PDO::CASE_LOWER: Forces column names to be lowercase.
PDO::CASE_NATURAL: Column names are in their original form
PDO::CASE_UPPER: Force column names to uppercase.
PDO::ATTR_ERRMODE: error message.
PDO::ERRMODE_SILENT: Does not display error information, only error code.
PDO::ERRMODE_WARNING: Display warning errors.
PDO::ERRMODE_EXCEPTION: Throws an exception.
PDO::ATTR_ORACLE_NULLS (valid not only for ORACLE, but also for other databases): ) specifies the corresponding value in php for the NULL value returned by the database.
PDO::NULL_NATURAL: unchanged.
PDO::NULL_EMPTY_STRING: Empty string is converted to NULL.
PDO::NULL_TO_STRING: NULL is converted to an empty string.
PDO::ATTR_STRINGIFY_FETCHES: Convert numeric values to strings when fetching. Requires bool.
PDO::ATTR_STATEMENT_CLASS: Set user-supplied statement class derived from PDOStatement. Cannot be used with persistent PDO instances. Requires array(string classname, array(mixed constructor_args)).
PDO::ATTR_AUTOCOMMIT (available in OCI, Firebird and MySQL): Whether to autocommit every single statement.
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY (available in MySQL): Use buffered queries.
$rs->setFetchMode(PDO::FETCH_ASSOC); in the example is PDOStatement::setFetchMode(), a declaration of the return type.
There are as follows:
PDO::FETCH_ASSOC -- associative array form
PDO::FETCH_NUM -- Numeric index array form
PDO::FETCH_BOTH -- Both are available in array form, which is the default
PDO::FETCH_OBJ -- in the form of an object, similar to the previous mysql_fetch_object()
For more return type declarations (PDOStatement::method name), see the manual.
insert, update, delete data,
$db->exec("Delete FROM `xxxx_menu` where mid=43");
Briefly summarize the above operations:
Query operations are mainly PDO::query(), PDO::exec(), PDO::prepare().
PDO::query() is mainly used for operations that return recorded results, especially Select operations.
PDO::exec() is mainly for operations that do not return a result set, such as Insert, Update, Delete and other operations. The result it returns is the number of columns affected by the current operation.
PDO::prepare() is mainly a preprocessing operation. You need to use $rs->execute() to execute the SQL statement in the preprocessing. This method can bind parameters and is quite powerful. It cannot be explained simply in this article. Everyone You can refer to manuals and other documentation.
The main operations for obtaining the result set are: PDOStatement::fetchColumn(), PDOStatement::fetch(), PDOStatement::fetchALL().
PDOStatement::fetchColumn() is a field of the first record specified in the fetch result. The default is the first field.
PDOStatement::fetch() is used to get a record.
PDOStatement::fetchAll() is to get all the record sets into one. To obtain the results, you can set the type of the required result set through PDOStatement::setFetchMode.
There are also two surrounding operations, one is PDO::lastInsertId() and PDOStatement::rowCount(). PDO::lastInsertId() returns the last insertion operation, and the primary key column type is the last auto-increment ID.
PDOStatement::rowCount() is mainly used for the result set affected by the Delete, Insert, and Update operations of PDO::query() and PDO::prepare(), and is invalid for the PDO::exec() method and Select operation.