This article illustrates the simple use of PDO in PHP5.2. Share to everyone for your reference, specific as follows:
First, PDO configuration
1, to ensure that PHP version of 5.2.5
2, find the dynamic extensions extension in php.ini, remove the semicolon in front of the Extension=php_pdo.dll
3, remove the corresponding database PDO extension before the semicolon, such as: Extension=php_pdo_mysql.dll
Ii. Database in the sample
CREATE TABLE tablename (
ID mediumint (8) UNSIGNED not NULL auto_increment,
str varchar (x) NOT null DEFAULT ' ", C5/>primary KEY (ID)
);
Iii. Examples of procedures
<?php $dsn = "Mysql:host=localhost;dbname=test";
$user = ' root ';
$passwd = ' 123456 '; try{$db = new PDO ($DSN, $user, $passwd);} catch (Pdoexception $e) {echo link database failed!
"; Print "Exception information:". $e->getmessage ().
"<br/>"; Print "Exception file:". $e->getfile ().
"<br/>"; Print "Exception line number:". $e->getline ().
"<br/>";
Exit ();
//$sql = "INSERT into tablename SET str = ' Hello '"; $count = $db->exec ($sql);
The return value is the number of rows affected//$sql = "DELETE from tablename WHERE str = ' Hello ' LIMIT 1"; $count = $db->exec ($sql); The return value is the number of rows affected//preprocessing needs to query SQL statements//$DB->setattribute (Pdo::attr_case, pdo::case_natural); Column names are in the original manner (field) $sql = "SELECT * FROM tablename WHERE ID <: ID and str =: string"; SQL statement (parameter binding method) $query = $db->prepare ($sql); preprocessing//using a set of binding parameters to execute the query $query->execute (Array (': Id ' =>1, ' ": String ' => ')); Processing statement (Parameter binding method)//$query->setfetchmode (PDO::FETCH_ASSOC); Associative array form (access to array content only through field name labels) while ($item = $query->fetch (pdo::fetch_assOC)//Loop get Data {echo $item [' id ']. ': '. $item [' str ']. "
<br/> ";
Print_r ($item); //Use another set of binding parameters, and then execute the query again $query->execute (Array (': Id ' <=10, ': String ' => ' HelloWorld ')); Processing statement (Parameter binding method)//$query->setfetchmode (PDO::FETCH_ASSOC); Associative array form (access to the contents of the array only through the name of the field) while ($item = $query->fetch (PDO::FETCH_ASSOC))//loop to get the data {echo $item [' id ']. ': '. $item [' St R ']. "
<br/> ";
Print_r ($item); } $db = null;
Release the database link?>
For more information on PHP-related content readers can view the site topics: "PHP operation Office Document tips summary (including word,excel,access,ppt)", "PHP date and Time usage summary", "PHP object-oriented Program Design Introductory Course", "PHP string (string) Summary of usage, Getting Started tutorial on Php+mysql database operations, and summary of common PHP database operations techniques
I hope this article will help you with the PHP program design.