php pdo operation database, phppdo database_PHP tutorial
Jul 12, 2016 am 08:53 AMphp pdo operates the database, phppdo database
POD extension is added in PHP5. This extension provides PHP built-in class PDO to access the database. Different databases use the same method name , solve the problem of inconsistent database connections.
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 implements the oracle database API instead of the PDO extension. There are also drivers for MySQL, PostgreSQL, ODBC, and Firebird, with more in development.
PDO installation
You can check whether the PDO extension is installed through PHP’s phpinfo() function.
1. Install PDO on Unix/Linux system
On Unix or Linux you need to add the following extension:
extension=pdo.so
2. Install pdo in Windows
PDO and all major drivers are shipped with PHP as shared extensions, to activate them simply edit the php.ini file and add the following extension:
extension=php_pdo.dll
In addition, there are various database extensions corresponding to the following:
<span class="pln"><span class="pun"> ;<span class="pln">extension<span class="pun">=<span class="pln">php_pdo_firebird<span class="pun">.<span class="pln">dll <span class="pln"><span class="pun"><span class="pln"><span class="pun"><span class="pln"><span class="pun"><span class="pln"><span class="pun">;<span class="pln">extension<span class="pun">=<span class="pln">php_pdo_informix<span class="pun">.<span class="pln">dll <span class="pun">;<span class="pln">extension<span class="pun">=<span class="pln">php_pdo_mssql<span class="pun">.<span class="pln">dll <span class="pun">;<span class="pln">extension<span class="pun">=<span class="pln">php_pdo_mysql<span class="pun">.<span class="pln">dll <span class="pun">;<span class="pln">extension<span class="pun">=<span class="pln">php_pdo_oci<span class="pun">.<span class="pln">dll <span class="pun">;<span class="pln">extension<span class="pun">=<span class="pln">php_pdo_oci8<span class="pun">.<span class="pln">dll <span class="pun">;<span class="pln">extension<span class="pun">=<span class="pln">php_pdo_odbc<span class="pun">.<span class="pln">dll <span class="pun">;<span class="pln">extension<span class="pun">=<span class="pln">php_pdo_pgsql<span class="pun">.<span class="pln">dll <span class="pun">;<span class="pln">extension<span class="pun">=<span class="pln">php_pdo_sqlite<span class="pun">.<span class="pln">dll</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
Open php.ini and remove the semicolons in front of all the lines above.
After setting these configurations, we need to restart PHP or Web server.
Next we use mysql as an example to use pdo:
<?<span>php $dbms='mysql'; //數(shù)據(jù)庫類型 $host='localhost'; //數(shù)據(jù)庫主機(jī)名 $dbName='test'; //使用的數(shù)據(jù)庫 $user='root'; //數(shù)據(jù)庫連接用戶名 $pass=''; //對(duì)應(yīng)的密碼 $dsn="$dbms:host=$host;dbname=$dbName"<span>; try<span> { $dbh = new PDO($dsn, $user, $pass); //初始化一個(gè)PDO對(duì)象 echo "連接成功<br/>"<span>; /*你還可以進(jìn)行一次搜索操作 foreach ($dbh->query('SELECT * from FOO') as $row) { print_r($row); //你可以用 echo($GLOBAL); 來看到這些值 } */ $dbh = null<span>; } catch (PDOException $e<span>) { die ("Error!: " . $e->getMessage() . "<br/>"<span>); } //默認(rèn)這個(gè)不是長連接,如果需要數(shù)據(jù)庫長連接,需要最后加一個(gè)參數(shù):array(PDO::ATTR_PERSISTENT => true) 變成這樣: $db = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true<span>)); ?></span></span></span></span></span></span></span></span>
Let’s take a look at the detailed introduction of pdo:
1. Predefined constants:
PDO::PARAM_BOOL (integer) | represents the Boolean data type. |
PDO::PARAM_NULL (integer) | represents the NULL data type in SQL. |
PDO::PARAM_INT (integer) | represents an integer type in SQL. |
PDO::PARAM_STR (integer) | represents CHAR, VARCHAR or other string types in SQL. |
PDO::PARAM_LOB (integer) | represents the large object data type in SQL. |
PDO::PARAM_STMT (integer) | represents a recordset type. It is not currently supported by any driver. |
PDO::PARAM_INPUT_OUTPUT (integer) | The specified parameter is an INOUT parameter of a stored procedure. This value must be bitwise ORed with an explicit PDO::PARAM_* data type. |
PDO::FETCH_LAZY (integer) | Specify the acquisition method and return each row in the result set as an object. The variable name of this object corresponds to the column name. PDO::FETCH_LAZY creates the object variable name for access. Not valid in PDOStatement::fetchAll(). |
PDO::FETCH_ASSOC (integer) | Specify the acquisition method and return each row in the corresponding result set as an array indexed by the column name. If the result set contains multiple columns with the same name, PDO::FETCH_ASSOC returns only one value per column name. |
PDO::FETCH_NAMED (integer) | Specify the acquisition method and return each row in the corresponding result set as an array indexed by the column name. If the result set contains multiple columns with the same name, PDO::FETCH_ASSOC returns an array containing values ??for each column name. |
PDO::FETCH_NUM (integer) | Specify the acquisition method and return each row in the corresponding result set as an array indexed by the column number, starting from column 0. |
PDO::FETCH_BOTH (integer) | Specify the acquisition method and return each row in the corresponding result set as an array indexed by column number and column name, starting from column 0. |
PDO::FETCH_OBJ (integer) | Specify the acquisition method and return each row in the result set as an object whose attribute name corresponds to the column name. |
PDO::FETCH_BOUND (integer) | Specify the acquisition method, return TRUE and assign the column value in the result set to the PHP variable bound through the PDOStatement::bindParam() or PDOStatement::bindColumn() method. |
PDO::FETCH_COLUMN (integer) | Specify the acquisition method and return the required column from the next row in the result set. |
PDO::FETCH_CLASS (integer) | Specify the acquisition method, return a new instance of the requested class, and map the column to the corresponding attribute name in the class. Note: If the attribute does not exist in the requested class, the __set() magic method is called |
PDO::FETCH_INTO (integer) | Specify the acquisition method, update an existing instance of the requested class, and map the column to the corresponding attribute name in the class. |
PDO::FETCH_FUNC (integer) | Allows data to be processed in completely custom ways on the fly. (Only valid in PDOStatement::fetchAll()). |
PDO::FETCH_GROUP (integer) | Return grouped by value. Typically used with PDO::FETCH_COLUMN or PDO::FETCH_KEY_PAIR. |
PDO::FETCH_UNIQUE (integer) | Only take unique values. |
PDO::FETCH_KEY_PAIR (integer) | Get a result set with two columns into an array, where the first column is the key name and the second column is the value. Available since PHP 5.2.3. |
PDO::FETCH_CLASSTYPE (integer) | Determine the class name based on the value of the first column. |
PDO::FETCH_SERIALIZE (integer) | Similar to PDO::FETCH_INTO, but represents the object as a serialized string. Available since PHP 5.1.0. Starting with PHP 5.3.0, if this flag is set, the class's constructor is never called. |
PDO::FETCH_PROPS_LATE (integer) | Call the constructor before setting properties. Available since PHP 5.2.0. |
PDO::ATTR_AUTOCOMMIT (integer) | If this value is FALSE , PDO will attempt to disable autocommit in order for the database connection to start a transaction. |
PDO::ATTR_PREFETCH (integer) | Set the prefetch size to balance speed and memory usage for your application. Not all database/driver combinations support setting the prefetch size. Larger prefetch sizes result in improved performance but also consume more memory. |
PDO::ATTR_TIMEOUT (integer) | Set the timeout seconds for connecting to the database. |
PDO::ATTR_ERRMODE (integer) | See the Errors and Error Handling section for more information about this property. |
PDO::ATTR_SERVER_VERSION (integer) | This is a read-only property; returns the version information of the database service connected to PDO. |
PDO::ATTR_CLIENT_VERSION (integer) | This is a read-only property; returns the version information of the client library used by the PDO driver. |
PDO::ATTR_SERVER_INFO (integer) | This is a read-only property. Returns some meta-information about the database service to which PDO is connected. |
PDO::ATTR_CONNECTION_STATUS (integer) | |
PDO::ATTR_CASE (integer) | Use constants like PDO::CASE_* to force column names to the specified case. |
PDO::ATTR_CURSOR_NAME (integer) | Gets or sets the name of the cursor to use. Very useful when using scrollable cursors and positioned updates. |
PDO::ATTR_CURSOR (integer) | Select the cursor type. PDO currently supports PDO::CURSOR_FWDONLY and PDO::CURSOR_SCROLL. Typically PDO::CURSOR_FWDONLY unless a scrollable cursor is really needed. |
PDO::ATTR_DRIVER_NAME (string) | Returns the driver name.
Example using PDO::ATTR_DRIVER_NAME: <?php if ($db->getAttribute(PDO::ATTR_DRIVER_NAME) == 'mysql') { echo "Running on mysql; doing something mysql specific here\n"; } ?> |
PDO::ATTR_ORACLE_NULLS (integer) | 在獲取數(shù)據(jù)時(shí)將空字符串轉(zhuǎn)換成 SQL 中的 NULL 。 |
PDO::ATTR_PERSISTENT (integer) | 請(qǐng)求一個(gè)持久連接,而非創(chuàng)建一個(gè)新連接。關(guān)于此屬性的更多信息請(qǐng)參見 連接與連接管理 。 |
PDO::ATTR_STATEMENT_CLASS (integer) | ? |
PDO::ATTR_FETCH_CATALOG_NAMES (integer) | 將包含的目錄名添加到結(jié)果集中的每個(gè)列名前面。目錄名和列名由一個(gè)小數(shù)點(diǎn)分開(.)。此屬性在驅(qū)動(dòng)層面支持,所以有些驅(qū)動(dòng)可能不支持此屬性。 |
PDO::ATTR_FETCH_TABLE_NAMES (integer) | 將包含的表名添加到結(jié)果集中的每個(gè)列名前面。表名和列名由一個(gè)小數(shù)點(diǎn)分開(.)。此屬性在驅(qū)動(dòng)層面支持,所以有些驅(qū)動(dòng)可能不支持此屬性。 |
PDO::ATTR_STRINGIFY_FETCHES (integer) | ? |
PDO::ATTR_MAX_COLUMN_LEN (integer) | ? |
PDO::ATTR_DEFAULT_FETCH_MODE (integer) | 自 PHP 5.2.0 起可用。 |
PDO::ATTR_EMULATE_PREPARES (integer) | 自 PHP 5.1.3 起可用。 |
PDO::ERRMODE_SILENT (integer) | 如果發(fā)生錯(cuò)誤,則不顯示錯(cuò)誤或異常。希望開發(fā)人員顯式地檢查錯(cuò)誤。此為默認(rèn)模式。關(guān)于此屬性的更多信息請(qǐng)參見 錯(cuò)誤與錯(cuò)誤處理 。 |
PDO::ERRMODE_WARNING (integer) | 如果發(fā)生錯(cuò)誤,則顯示一個(gè) PHP E_WARNING 消息。關(guān)于此屬性的更多信息請(qǐng)參見 錯(cuò)誤與錯(cuò)誤處理。 |
PDO::ERRMODE_EXCEPTION (integer) | 如果發(fā)生錯(cuò)誤,則拋出一個(gè) PDOException 異常。關(guān)于此屬性的更多信息請(qǐng)參見 錯(cuò)誤與錯(cuò)誤處理。 |
PDO::CASE_NATURAL (integer) | 保留數(shù)據(jù)庫驅(qū)動(dòng)返回的列名。 |
PDO::CASE_LOWER (integer) | 強(qiáng)制列名小寫。 |
PDO::CASE_UPPER (integer) | 強(qiáng)制列名大寫。 |
PDO::NULL_NATURAL (integer) | ? |
PDO::NULL_EMPTY_STRING (integer) | ? |
PDO::NULL_TO_STRING (integer) | ? |
PDO::FETCH_ORI_NEXT (integer) | 在結(jié)果集中獲取下一行。僅對(duì)可滾動(dòng)游標(biāo)有效。 |
PDO::FETCH_ORI_PRIOR (integer) | 在結(jié)果集中獲取上一行。僅對(duì)可滾動(dòng)游標(biāo)有效。 |
PDO::FETCH_ORI_FIRST (integer) | 在結(jié)果集中獲取第一行。僅對(duì)可滾動(dòng)游標(biāo)有效。 |
PDO::FETCH_ORI_LAST (integer) | 在結(jié)果集中獲取最后一行。僅對(duì)可滾動(dòng)游標(biāo)有效。 |
PDO::FETCH_ORI_ABS (integer) | 根據(jù)行號(hào)從結(jié)果集中獲取需要的行。僅對(duì)可滾動(dòng)游標(biāo)有效。 |
PDO::FETCH_ORI_REL (integer) | 根據(jù)當(dāng)前游標(biāo)位置的相對(duì)位置從結(jié)果集中獲取需要的行。僅對(duì)可滾動(dòng)游標(biāo)有效。 |
PDO::CURSOR_FWDONLY (integer) | 創(chuàng)建一個(gè)只進(jìn)游標(biāo)的 PDOStatement 對(duì)象。此為默認(rèn)的游標(biāo)選項(xiàng),因?yàn)榇擞螛?biāo)最快且是 PHP 中最常用的數(shù)據(jù)訪問模式。 |
PDO::CURSOR_SCROLL (integer) | 創(chuàng)建一個(gè)可滾動(dòng)游標(biāo)的 PDOStatement 對(duì)象。通過 PDO::FETCH_ORI_* 常量來控制結(jié)果集中獲取的行。 |
PDO::ERR_NONE (string) | 對(duì)應(yīng) SQLSTATE '00000',表示 SQL 語句沒有錯(cuò)誤或警告地成功發(fā)出。當(dāng)用 PDO::errorCode() 或 PDOStatement::errorCode() 來確定是否有錯(cuò)誤發(fā)生時(shí),此常量非常方便。在檢查上述方法返回的錯(cuò)誤狀態(tài)代碼時(shí),會(huì)經(jīng)常用到。 |
PDO::PARAM_EVT_ALLOC (integer) | 分配事件 |
PDO::PARAM_EVT_FREE (integer) | 解除分配事件 |
PDO::PARAM_EVT_EXEC_PRE (integer) | 執(zhí)行一條預(yù)處理語句之前觸發(fā)事件。 |
PDO::PARAM_EVT_EXEC_POST (integer) | 執(zhí)行一條預(yù)處理語句之后觸發(fā)事件。 |
PDO::PARAM_EVT_FETCH_PRE (integer) | 從一個(gè)結(jié)果集中取出一條結(jié)果之前觸發(fā)事件。 |
PDO::PARAM_EVT_FETCH_POST (integer) | 從一個(gè)結(jié)果集中取出一條結(jié)果之后觸發(fā)事件。 |
PDO::PARAM_EVT_NORMALIZE (integer) | 在綁定參數(shù)注冊(cè)允許驅(qū)動(dòng)程序正?;兞棵麜r(shí)觸發(fā)事件。 |