PHP连接数据库的另外一种艺术—PDO

世家连连数据库大多数当用的都是mysql_connect()这样的吧,当然,肯定也有非是的啊,在php5以后的版本被多了mysqli_connect(),这样的比mysql_connect()高效,其实这其间还蕴藏一个php自己写的坐类 
$conn=new
mysqli(),我记得是这样的,大家好试行看,在此我一旦说的莫是这些连方式,而是另外一栽不畏PDO来连接数据库
具体方法我整理出来,和豪门享受一下,共同进步!

/**
*亟待加载pdo模块,找到
 ;extension=php_pdo.dll
 ;extension=php_pdo_firebird.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
把前的;都去丢,相信这大家还见面的即使无多说
*/

eg1、Connecting to mysql

1 <?php
2 $dbh = newPDO('mysql:host=localhost;dbname=test',  $user, $pass);
3 ?>

eg2、Handling connection errors

 1 <?php
 2 try {
 3 $dbh = new PDO('mysql:host=localhost;dbname=test',  $user, $pass);
 4 foreach ($dbh->query('SELECT * from FOO') as $row) {
 5 print_r($row);
 6 }
 7 $dbh = null;
 8 } catch (PDOException $e) {
 9 print  "Error!: " . $e->getMessage() . "<br/>";
10 die();
11 }
12 ?>  

eg3、Closing a connection

1 <?php
2 $dbh = newPDO('mysql:host=localhost;dbname=test',  $user, $pass);
3 // use the connection here
4 
5 // and now we're done; close  it
6 $dbh = null;
7 ?>

eg4、Persistent connections 永久连接

1 <?php
2 $dbh = new PDO('mysql:host=localhost;dbname=test',  $user, $pass, array(PDO_ATTR_PERSISTENT =>  true
3 ));  

eg5、Executing a batch in a transaction  在作业中的批判处理实施

 1 <?php
 2 try {
 3 $dbh = new PDO ('odbc:SAMPLE', 'db2inst1', 'ibmdb2', 
 4 array(PDO_ATTR_PERSISTENT =>  true));
 5 echo  "Connected\n";
 6 $dbh->setAttribute(PDO_ATTR_ERRMODE, PDO_ERRMODE_EXCEPTION);
 7 
 8 $dbh->beginTransaction();
 9 $dbh->exec("insert into staff (id, first, last)  values (23, 'Joe', 'Bloggs')");
10 $dbh->exec("insert  into salarychange (id, amount, changedate) 
11 values (23, 50000,  NOW())");
12 $dbh->commit();
13 
14 } catch (Exception $e) {
15 $dbh->rollBack();
16 echo "Failed: " . $e->getMessage();
17 }
18 ?> 

eg6、Repeated inserts using prepared statements
-1 重复使用准备好的讲话插入(即绑定参数)

 1 <?php
 2 $stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value)  VALUES (:name, :value)");
 3 $stmt->bindParam(':name', $name);
 4 $stmt->bindParam(':value', $value);
 5 
 6 // insert one row
 7 $name = 'one';
 8 $value = 1;
 9 $stmt->execute();
10 
11 // insert another row with  different values
12 $name = 'two';
13 $value = 2;
14 $stmt->execute();
15 ?> 

eg7、Repeated inserts using prepared statements -2

 1 <?php
 2 $stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value)  VALUES (?, ?)");
 3 $stmt->bindParam(1, $name);
 4 $stmt->bindParam(2, $value);
 5 
 6 // insert one row
 7 $name = 'one';
 8 $value = 1;
 9 $stmt->execute();
10 
11 // insert another row with  different values
12 $name = 'two';
13 $value = 2;
14 $stmt->execute();
15 ?> 

eg8、 Fetching data using prepared statements 提取数额以准备好之口舌
(这个例子对得到之形式提供一个键值的数目。用户输入是机关进行加引号,所以无设有一个SQL注入攻击的生死存亡)

1 <?php
2 $stmt = $dbh->prepare("SELECT * FROM REGISTRY where name =  ?");
3 if ($stmt->execute(array($_GET['name']))) {
4 while ($row = $stmt->fetch()) {
5 print_r($row);
6 }
7 }
8 ?> 

说到底自己提供一些谈得来写的顺序,不熟悉的爱侣相信小用的呀,会的即使可以无看呀

<?php
//插入数据
try {
   $dbh = new PDO('mysql:host=localhost;dbname=gbook',"root","123456");

/*
*第一种方法:
*/   
$stmt = $dbh->prepare("INSERT INTO guestbook(title, name, contents)  VALUES (:title,:name,:contents)");
   $title = 'Hello';
   $name = "CWL";
   $contents="Hello World!";
   $stmt->execute(array(':title' => $title, ':name' => $name,'contents'=>$contents));

/*
此为第二种方法,也可,与上种方法相比,省去对 $stmt->bindParam() 的调用。
   $stmt = $dbh->prepare("INSERT INTO guestbook(title, name, contents)  VALUES (:title,:name,:contents)");
   $title = 'Hello';
   $name = "CWL";
   $contents="Hello World!";
   $stmt->bindParam(':title', $title);
   $stmt->bindParam(':name', $name);
   $stmt->bindParam(':contents', $contents);
   $stmt->execute();
*/
} catch (PDOException $e) {
    print  "Error!: " . $e->getMessage() . "<br/>";
    die();
}

//查询数据
/*
try {
    foreach ($dbh->query('SELECT * from guestbook') as $row) {
        print_r($row);
    }
    $dbh = null;
} catch (PDOException $e) {
    print  "Error!: " . $e->getMessage() . "<br/>";
    die();
}
*/
?>

正文自:http://hi.baidu.com/caowlong/blog/item/ea650c464d942441510ffeb7.html

 

 

PHP 交流群:120372298    大家一齐念书

相关文章