• 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    大家一起学习

  • 相关阅读:
    Django视图
    Django路由系统
    Django概述,配置文件,web框架本质,Django框架
    Mysql
    R语言之常用函数
    从PCA、PLS-DA、OPLS-DA学习线性代数和矩阵
    算法第一关
    别人处理二代测序的流程
    reportlab包使用指南
    Python 文本(txt) 转换成 EXCEL(xls)
  • 原文地址:https://www.cnblogs.com/guhao/p/2948800.html
Copyright © 2020-2023  润新知