Simple use of PDO in PHP5.2, php5.2pdo usage

Source: Internet
Author: User

Simple use of PDO in PHP5.2, php5.2pdo usage

This document describes how to use PDO in PHP5.2. We will share this with you for your reference. The details are as follows:

1. PDO Configuration

1. Make sure that the PHP version is 5.2.5 or later.
2. Find the Dynamic Extensions extension in php. ini and remove the semicolon before extension = php_pdo.dll.
3. Remove the semicolon before the corresponding database PDO extension, for example, extension = php_pdo_mysql.dll.

2. databases in the example

CREATE TABLE tablename (  id mediumint(8) UNSIGNED NOT NULL AUTO_INCREMENT,  str varchar(50) NOT NULL DEFAULT '''',  PRIMARY KEY (id));

Iii. program examples

<? Php $ dsn = "mysql: host = localhost; dbname = test"; $ user = ''root''; $ passwd = ''000000 ''; try {$ db = new PDO ($ dsn, $ user, $ passwd);} catch (PDOException $ e) {echo "failed to connect to the database! "; Print" exception information :". $ e-> getMessage (). "<br/>"; print "exception file :". $ e-> getFile (). "<br/>"; print "abnormal row number :". $ e-> getLine (). "<br/>"; exit ();} // $ SQL = "INSERT INTO tablename SET str = 'hello ''"; // $ count = $ db-> exec ($ SQL ); // The returned value is the number of affected rows. // $ SQL = "DELETE FROM tablename WHERE str = 'hello' 'limit 1 "; // $ count = $ db-> exec ($ SQL); // The returned value is the number of affected rows // The SQL statement to be queried by preprocessing // $ db-> setAttribute (PDO:: ATTR_CASE, PDO: CASE_NATURAL ); // The column name follows the original method (field) $ SQL = "SELECT * FROM tablename WHERE id <: id AND str =: string "; // SQL statement (parameter binding method) $ query = $ db-> prepare ($ SQL ); // pre-processing // use a set of BIND parameters to perform a query once $ query-> execute (array ('': id'' => 1 ,'': string ''=> ''hello''); // processing Statement (parameter binding method) // $ query-> setFetchMode (PDO: FETCH_ASSOC ); join array form (only accessing the array content through the field name subscript) while ($ item = $ query-> fetch (PDO: FETCH_ASSOC )) // obtain data cyclically {echo $ item [''id'']. ":". $ item [''str'']. "<br/>"; // print_r ($ Item) ;}// bind parameters to another group, and then query $ query-> execute (array ('': id'' <= 10, '': string'' => ''helloworld'); // processing Statement (parameter binding method) // $ query-> setFetchMode (PDO: FETCH_ASSOC ); join array form (only accessing the array content through the field name subscript) while ($ item = $ query-> fetch (PDO: FETCH_ASSOC )) // obtain data cyclically {echo $ item [''id'']. ":". $ item [''str'']. "<br/>"; // print_r ($ item);} $ db = null; // release the database link?>

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.