Php database abstraction layer PDO_PHP tutorial-php Tutorial

Source: Internet
Author: User
Php database abstraction layer PDO. The following describes how to use the database abstraction layer PDO: PDO (PHPDataObjects) is a lightweight PHP extension that provides a data access abstraction layer. Also, PDO can only introduce the use of the database abstraction layer PDO below:

PDO (PHP Data ObjectsIs a lightweight PHP extension that provides a data access abstraction layer. Also, PDO can only be used in PHP5.0 or later versions.

The following describes the predefined constants commonly used by PDO:

PDO: PARAM_BOOL(Integer) Indicates the Boolean data type.

PDO: PARAM_NULL(Integer) Indicates the SQL statement whose data type is NULL.

PDO: PARAM_INT(Integer) Indicates the SQL statement of the integer data type.

PDO: PARAM_STR(IntegerSQL statement that represents the data type of char varchar or other strings

PDO: PARAM_LOB(Integer) Indicates the SQL statement of the object data type.

PDO: FETCH_LAZY(Integer) The specified method of obtaining. each row of the result set should be returned as the variable name of an object, corresponding to its field name.

PDO: FETCH_ORI_NEXT(Integer)Returns the next row of the result set.

PDO: FETCH_ORI_PRIOR(Integer)Get the row before the result set

PDO: FETCH_ORI_FIRST(Integer)The first row of the result set.

PDO: FETCH_ORI_LAST(Integer)Returns the last row of the result set.

PDO: ATTR_PERSISTENT(IntegerInstead of creating a new connection.

Basic usage of PDO:

Use PDO to connect to the database (MySQL only ):

The code is as follows:


$ Dbh = new PDO ('MySQL: host = localhost; dbname = test', $ user, $ pass );
?>


The following code is used to handle MySQL connection errors:

The code is as follows:


Try {
$ Dbh = new PDO ('MySQL: host = localhost; dbname = test', $ user, $ pass );
Foreach ($ dbh-> query ('select * from Foo') as $ row ){
Print_r ($ row );
}
$ Dbh = null;
} Catch (PDOException $ e ){
Print "Error! : ". $ E-> getMessage ()."
";
Die ();
}
?>


The following are two examples of repeated insert statements:

The code is as follows:


$ Stmt = $ dbh-> prepare ("insert into registry (name, value) VALUES (: name,: value )");
$ Stmt-> bindParam (': name', $ name );
$ Stmt-> bindParam (': value', $ value );

// Insert one row
$ Name = 'one ';
$ Value = 1;
$ Stmt-> execute ();

// Insert another row with different values
$ Name = 'two ';
$ Value = 2;
$ Stmt-> execute ();
?>


The code is as follows:


$ Stmt = $ dbh-> prepare ("insert into registry (name, value) VALUES (?, ?) ");
$ Stmt-> bindParam (1, $ name );
$ Stmt-> bindParam (2, $ value );

// Insert one row
$ Name = 'one ';
$ Value = 1;
$ Stmt-> execute ();

// Insert another row with different values
$ Name = 'two ';
$ Value = 2;
$ Stmt-> execute ();
?>


Query a database:

The code is as follows:


$ Stmt = $ dbh-> prepare ("SELECT * from registry where name =? ");
If ($ stmt-> execute (array ($ _ GET ['name']) {
While ($ row = $ stmt-> fetch ()){
Print_r ($ row );
}
}
?>

PDO (PHP Data Objects) is a lightweight PHP extension that provides a Data access abstraction layer. Also, PDO can only be used in...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.