pdo例程(预处理)

<?php
//更新反馈处理:使用异常形式的处理,代替了or die,更便捷!
/**
* PDO操作示例,在预处理上跟mysqli相似,不过,比mysqli更便捷
*/
header('Content-type=text/html;charset=utf-8');
try {
     $pdo = new PDO('mysql:host=localhost;dbname=test', 'root', 'root', array(PDO::ATTR_ERRMODE => pdo::ERRMODE_EXCEPTION));
     //可以直接在这里写所有select,insert,update,delete的执行处理,抛出同一个个例外。
} catch (PDOException $e) {
     echo $e->getMessage();
     exit();
}

try {
     /**====================================================================================
     * 处理select语句
     * =====================================================================================
     */
     $stmt = $pdo->prepare('select * from user where id > :id');               //不用or die 了
     $stmt->execute(array(':id' => 7));     //不绑定的快捷方法
    
     //用问号占位符:
     $stmt = $pdo->prepare('select * from user where id > ?');
      $stmt->execute(array(7));               //位置对应,这种方法应该更普遍

     $rows = $stmt->rowCount();
     if($rows == 0) {
          echo '没能查出数据';
     }else {
            //记录条数:
          echo '总共记录条数:' . $stmt->rowCount() . '<br>';                    //建议用select count(*)代替,因为不是所有数据库都支持
    
          //字段总数:
          echo '总字段数:' . $stmt->columnCount() . '<br>';
         
          //所有字段信息,非指针
          echo '字段信息:';
          for($i = 0; $i < $stmt->columnCount(); $i++) {
               var_dump($stmt->getColumnMeta($i));
          }
    
          //获取所有记录
          /**
          echo '获取所有记录(逐行获取):<br>';
          while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
               foreach ($row as $k => $v) {
                    echo $k . '->' . $v . '-----';
               }
               echo '<br>';
          }
          **/
    
          echo '获取所有记录(一次性全部获取):';
          var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
     }
   
    
    
     /**=====================================================================================
     * 处理insert语句
     *======================================================================================
     */
     $stmt = $pdo->prepare('insert into user(username,password) values(:username,:password)');
     $stmt->execute(array(':username' => 'ini', ':password' => '884456'));
    
     $insertID = $pdo->lastInsertId();
     if($insertID == 0) {
          echo '未能成功插入数据';
     }else {
          //获取插入的ID
          echo '插入后的新增ID为:' .$insertID . '<br>';
          //影响行数
          echo '影响行数:' . $stmt->rowCount() . '<br>';                    //rowCount()既可以返回select的结果行数,也可以返回insert,update,delete的行数
     }

     /**
     *=====================================================================================
     * 处理update语句
     *=====================================================================================
     */
     $stmt = $pdo->prepare('update user set username = :username where id = :id');
     $stmt->execute(array(':username' => 'tomcat', ':id' => 23));
    
     $affectedRows = $stmt->rowCount();
     if($affectedRows == 0) {
          echo '未能成功更新数据';
     }else {
          //影响行数
          echo '影响行数:' . $stmt->rowCount() . '<br>';
     }

     /**
     *=====================================================================================
     * 处理delete语句
     *=====================================================================================
     */
     $stmt = $pdo->prepare('delete from user where username = :username');
     $stmt->execute(array(':username' => 'ini'));
 
     $affectedRows = $stmt->rowCount();
     if($affectedRows == 0) {
          echo '未能成功更新数据';
     }else {
          //影响行数
          echo '影响行数:' . $stmt->rowCount() . '<br>';
     }
} catch (PDOException $e) {
     echo $e->getMessage();
     exit();
}

posted on 2013-03-13 11:18  bgwan  阅读(258)  评论(0编辑  收藏  举报

导航