sqlitePHP连接数据库的其余一种情势—PDO

咱们连连数据库大多数相应用的都是mysql_connect()这样的呢,当然,肯定也有不是的哟,在php5未来的版本中加进了mysqli_connect(),这样的比mysql_connect()高效,其实这中间还蕴藏一个php自己写的松手类 
$conn=new
mysqli(),我记念是这么的,我们可以试行看,在这里自己要说的不是这些连接形式,而是此外一种即PDO来连接数据库
具体方法我整理出来,和大家分享一下,共同提升!

/**
*需要加载pdo模块,找到
sqlite, ;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    我们共同学习

网站地图xml地图