PDO简单使用示例

PDO简单使用示例基础操作:

<?php   $dbh = new PDO('mysql:host=localhost;dbname=access_control' 'root' '');     $dbh->setAttribute(PDO::ATTR_ERRMODE PDO::ERRMODE_EXCEPTION);     $dbh->exec('set names utf8');    /*添加*/   //$sql = "INSERT INTO `user` SET `login`=:login AND `password`=:password";    $sql = "INSERT INTO `user` (`login` `password`)VALUES (:login :password)";   $stmt = $dbh->prepare($sql);  $stmt->execute(array(':login'=>'kevin2'':password'=>''));     echo $dbh->lastinsertid();     /*修改*/   $sql = "UPDATE `user` SET `password`=:password WHERE `user_id`=:userId";     $stmt = $dbh->prepare($sql);     $stmt->execute(array(':userId'=>'7' ':password'=>'4607e782c4d86fd5364d7e4508bb10d9'));     echo $stmt->rowCount();    /*删除*/   $sql = "DELETE FROM `user` WHERE `login` LIKE 'kevin_'"; //kevin%     $stmt = $dbh->prepare($sql);     $stmt->execute();     echo $stmt->rowCount();     /*查询*/   $login = 'kevin%';     $sql = "SELECT * FROM `user` WHERE `login` LIKE :login";     $stmt = $dbh->prepare($sql);     $stmt->execute(array(':login'=>$login));     while($row = $stmt->fetch(PDO::FETCH_ASSOC)){         print_r($row);     }     print_r( $stmt->fetchAll(PDO::FETCH_ASSOC));    ?> 
  1. 建立数据库连接:
<?php   $dbh=newPDO('mysql:host=localhost;port=3306; dbname=test'$user$passarray(   PDO::ATTR_PERSISTENT=>true   ));   ?> 

持久性链接则指定:PDO::ATTR_PERSISTENT=>true

  1. 捕捉错误
<?php   try{   $dbh=newPDO('mysql:host=localhost;dbname=test'$user$pass);    $dbh->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);    $dbh->exec("SET CHARACTER SET utf8");   $dbh=null; //断开连接   }catch(PDOException$e){   print"Error!:".$e->getMessage()."<br/>";   die();   }   ?> 
  1. 事务处理
<?php   try{   $dbh->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);    $dbh->beginTransaction();//开启事务   $dbh->exec("insertintostaff(idfirstlast)values(23'Joe''Bloggs')");   $dbh->exec("insertintosalarychange(idamountchangedate)   values(2350000NOW())");   $dbh->commit();//提交事务    }catch(Exception$e){   $dbh->rollBack();//错误回滚   echo"Failed:".$e->getMessage();   }   ?> 
  1. 错误处理

a. 静默模式(默认模式)

$dbh->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_SILENT); //不显示错误  $dbh->setAttribute(PDO::ATTR_ERRMODE PDO::ERRMODE_WARNING);//显示警告错误,并继续执行  $dbh->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);//产生致命错误,PDOException 
<?php   try{        $dbh = new PDO($dsn $user $password);        $sql = 'Select * from city where CountryCode =:country';        $dbh->setAttribute(PDO::ATTR_ERRMODE PDO::ERRMODE_WARNING);        $stmt = $dbh->prepare($sql);        $stmt->bindParam(':country' $country PDO::PARAM_STR);        $stmt->execute();        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {           print $row['Name'] . "/t";        }     }   // if there is a problem we can handle it here     catch (PDOException $e)  {        echo 'PDO Exception Caught.  ';        echo 'Error with the database: <br />';        echo 'SQL Query: ' $sql;       echo 'Error: ' . $e->getMessage();     }    ?> 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/264130.html

(0)
上一篇 2022年6月7日
下一篇 2022年6月7日

相关推荐

发表回复

登录后才能评论