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?>