search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

PHP PDO

Collection 1351
Read 450446
update time 2016-09-11

The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for PHP to access databases.

PDO provides a data access abstraction layer, which means that no matter which database is used, the same functions (methods) can be used to query and obtain data.

PDO is released with PHP5.1 and can also be used in the PECL extension of PHP5.0. It cannot run on previous PHP versions.


PDO Installation

You can check whether the PDO extension is installed through PHP's phpinfo() function.

Installing PDO on Unix systems

On Unix or Linux you need to add the following extensions:

extension=pdo.so

Windows users

PDO and all major Drivers are distributed 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:
;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

In After setting these configurations, we need to restart PHP or the web server.

Next let’s take a look at specific examples. The following is an example of using PDO to connect to a MySql database:

<?php
$dbms='mysql';     //数据库类型
$host='localhost'; //数据库主机名
$dbName='test';    //使用的数据库
$user='root';      //数据库连接用户名
$pass='';          //对应的密码
$dsn="$dbms:host=$host;dbname=$dbName";


try {
    $dbh = new PDO($dsn, $user, $pass); //初始化一个PDO对象
    echo "连接成功<br/>";
    /*你还可以进行一次搜索操作
    foreach ($dbh->query('SELECT * from FOO') as $row) {
        print_r($row); //你可以用 echo($GLOBAL); 来看到这些值
    }
    */
    $dbh = null;
} catch (PDOException $e) {
    die ("Error!: " . $e->getMessage() . "<br/>");
}
//默认这个不是长连接,如果需要数据库长连接,需要最后加一个参数:array(PDO::ATTR_PERSISTENT => true) 变成这样:
$db = new PDO($dsn, $user, $pass, array(PDO::ATTR_PERSISTENT => true));

?>

It’s very simple. Next, let’s take a look at the specifics of PHP PDO. Description:

  • Predefined constants

  • PHP PDO connection connection management

  • PHP PDO transaction and Automatic submission

  • PHP PDO prepared statements and stored procedures

  • PHP PDO errors and error handling

  • PHP PDO Large Objects (LOBs)

  • PDO Class:

    • PDO::beginTransaction — Start a transaction

    • PDO::commit — Commit a transaction

    • PDO::__construct — Create a PDO instance that represents a database connection

    • PDO::errorCode — Get the SQLSTATE related to the last operation of the database handle

    • PDO::errorInfo — Return the error information of the last operation on the database

    • PDO::exec — Execute a SQL statement and return the number of affected rows

    • PDO::getAttribute — Retrieve attributes of a database connection

    • PDO::getAvailableDrivers — Returns an array of available drivers

    • PDO::inTransaction — Checks whether within a transaction

    • PDO::lastInsertId — Returns the ID or sequence value of the last inserted row

    • PDO::prepare — Prepares the SQL statement to be executed and returns a PDOStatement object

    • PDO::query — executes a SQL statement and returns a PDOStatement object, which can be understood as a result set

    • PDO::quote — is a SQL statement Add quotes to the string.

    • PDO::rollBack — rollback a transaction

    • PDO::setAttribute — set attributes

  • PDOStatement class:

    • PDOStatement::bindColumn — Bind a column to a PHP variable

    • PDOStatement::bindParam — Bind a parameter to the specified variable name

    • PDOStatement::bindValue — Bind a value to a parameter

    • PDOStatement::closeCursor — Close the cursor so that the statement can be executed again.

    • PDOStatement::columnCount — Returns the number of columns in the result set

    • PDOStatement::debugDumpParams —Print a SQL preprocessing command

    • PDOStatement::errorCode — Get the SQLSTATE related to the last statement handle operation

    • PDOStatement::errorInfo — Get the extension related to the last statement handle operation Error message

    • PDOStatement::execute — Execute a prepared statement

    • PDOStatement::fetch — Get the next row from the result set

    • PDOStatement::fetchAll — Returns an array containing all rows in the result set

    • PDOStatement::fetchColumn — Returns a single column from the next row in the result set .

    • PDOStatement::fetchObject — Gets the next row and returns it as an object.

    • PDOStatement::getAttribute — Retrieve a statement attribute

    • PDOStatement::getColumnMeta — Return metadata for a column in the result set

    • PDOStatement::nextRowset — Advances to the next rowset in a multi-rowset statement handle

    • PDOStatement::rowCount — Returns affected by the previous SQL statement Number of rows

    • PDOStatement::setAttribute — Sets a statement attribute

    • PDOStatement::setFetchMode — Sets the default fetch mode for a statement.

Hot AI Tools
Undress AI Tool
Undress AI Tool

Undress images for free

AI Clothes Remover
AI Clothes Remover

Online AI tool for removing clothes from photos.

Undresser.AI Undress
Undresser.AI Undress

AI-powered app for creating realistic nude photos

ArtGPT
ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT
Stock Market GPT

AI powered investment research for smarter decisions

Popular tool
Notepad++7.3.1
Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version
SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1
Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6
Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version
SublimeText3 Mac version

God-level code editing software (SublimeText3)