mysql_PDO笔记

mysql连接

dsn:data source name

$db = [
    'dsn'=>'mysql:host=localhost;dbname=mf_demo;port=3306',
    'name' => 'root',
    'passwd'=>'m123456',
    'opts' => [PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION] // 可选:调优参数
  ];
  
  try{
    $pdo = new PDO($db['dsn'],$db['name'],$db['passwd'],$db['opts']);
  }catch(PDOException $e){
    echo $e->getMessage();
    exit;
  }
pdo执行sql语句的三种方法
  • query(),执行select语句,返回结果集
try{
  $select_sql = "select * from mf_loving";
  $stmt = $pdo->query($select_sql);
  print_r($stmt);
}catch(PDOException $e){
  $e->getMessage();
}
  • exec(),执行update,delete,insert…返回影响的行数
try{
    $update_sql = "update mf_loving set name = 'shiyuan' where id = 6";
    $affected_rows = $pdo->exec($update_sql);
    echo '执行成功,影响行数:' . $affected_rows;
  }catch(PDOException $e){
    echo $e->getMessage();
  }
  • prepare(),可执行所有的sql语句
$update_sql = "update mf_loving set name = ? where id = ?";
$stmt = $pdo->prepare($update_sql);
try{
  $res = $stmt->execute(['shiyuan',6]);
}catch(PDOException $e){
  echo $e->getMessage();	
}
常用stmt方法
 /**
 * 设置获取数据的模式
 * @ params: PDO::FETCH_NUM 或 PDO::FETCH_ASSOC
 */
 $stmt->setFetchMode(PDO::FETCH_NUM);  
$data = $stmt->fetch(); // 一条数据 
$data = $stmt->fetchAll(); // 所有数据
 $rowLen = $stmt->rowCount(); // 获取数据的行数
 $columnLen = $stmt->columnCount(); // 获取字段数
// 获取返回数据的字段信息
 $fileds = [];
 for($i=0; $i<$columnLen; $i++){
   $filed = $stmt->getColumnMeta($i);
   array_push($fileds,$filed['name']);
 }
 print_r($fileds);
常用的pdo方法
// 获取最后插入的ID
$last_insert_id = $pdo->lastInsertId();
事务处理
 try{
 	// 开启事务处理
    $pdo->beginTransaction();
    $price = 50;
    $sql = "update mf_account set account =account+ ? where name = ?";
    $stmt = $pdo->prepare($sql);
    // 张三转出
    $affected_rows = $stmt->execute([-$price,'zhangsan']);
    echo ($affected_rows ? $affected_rows : '张三转出失败') . '<br>'; 
    // 李四转入
    $affected_rows = $stmt->execute([$price,'lisi']);
    echo ($affected_rows ? $affected_rows : '李四转出失败') . '<br>'; 
    // 无错误,提交事务
    $pdo->commit();
    echo '交易成功';
  }catch(PDOException $e){
    echo $e->getMessage();
    // 有错误,回滚操作
    $pdo->rollback();
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值