pdo bind

  1. <?php      
  2. /* 
  3.  * 开启pdo,php.ini扩展打开即可 
  4. 预处理: 
  5.     1,?    绑定 $stmt->bindparam('1',$name,PDO::PARAM_STR);  //从1始 
  6.     2,:name   $stmt->bindparam(':name',$name);  第三个参数可写可不写 
  7.     3,execute(数组); 
  8.     如何获得受影响行,插入ID rowCount()   lastInsertId() 
  9.   
  10. 结果集遍历:4种  
  11.     1,query来foreach   $result = $pdo->query()  可直接foreach $result 
  12.     2,bindColumn 绑定列  while($stmt->fetch()) 
  13.     3,fetch     while($row=$stmt->fetch(PDO::FETCH_ASSOC)) 
  14.     4,fetchAll  $stmt->fetchall()  返回的是个数组 
  15.     如何获取字段[了解] 
  16.  */  
  17.    
  18. try{  
  19.     $dsn = 'mysql:host=localhost;dbname=test';   //手册查询  
  20.     $pdo = new Pdo($dsn,'root','abc123');  //第四个参数 可初始化设置  
  21.     $pdo->query('set names utf8');  //字符集  
  22.     $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);  
  23.    
  24.     //字段  select id,username,password from user  
  25.     $stmt = $pdo->prepare('desc user');  
  26.     $stmt->execute();  
  27.     $j = $stmt->columnCount();  //统计列数  
  28.        
  29.     for($i=0;$i<$j;$i++){  
  30.         echo '<pre>';  
  31.         var_dump($stmt->getColumnMeta($i));  
  32.         echo '</pre>';  
  33.     }  
  34.     $stmt->setFetchMode(PDO::FETCH_NUM);  
  35.         
  36.     //PDO::FETCH_NUM  
  37.     //PDO::FETCH_ASSOC  
  38.        
  39. /*  可直接遍历 
  40.     $result = $pdo->query('select username,password from user'); 
  41.     foreach($result as $key=>$value){ 
  42.         echo $value['username'].$value['password'].'<br>'; 
  43.     } 
  44.  */  
  45. /*  execute(数组);  fetchAll  遍历 
  46.     $stmt = $pdo->prepare('select username,password from user where id>:id and username=:username'); 
  47.     $stmt->execute(array(':id'=>120,':username'=>'养生'));  //带不带冒号均可 
  48.     $result = $stmt->fetchAll(); 
  49.     foreach($result as $value){ 
  50.         echo $value['password'].'<br>'; 
  51.     } 
  52.  */  
  53. /*  //:id  fetch遍历 
  54.     $stmt = $pdo->prepare('select username,password from user where id>:id'); 
  55.     $stmt->bindparam(':id',$id,PDO::PARAM_INT); 
  56.     $id = 122; 
  57.     $result = $stmt->execute(); 
  58.     while($row = $stmt->fetch()){ 
  59.         echo $row['username'].$row['password']; 
  60.         echo '<br>'; 
  61.     } 
  62.  */  
  63. /*  //绑定列 bindcolumn 
  64.     $stmt = $pdo->prepare('select username,password from user where id>?'); 
  65.   
  66. //  $stmt->bindparam('1',$p1,PDO::PARAM_STR); 
  67. //  $stmt->bindparam('2',$p2,PDO::PARAM_STR); 
  68.     $stmt->bindparam('1',$p3,PDO::PARAM_INT); 
  69.   
  70. //  $p1 = 'username'; 
  71. //  $p2 = 'password'; 
  72.     $p3 = 120; 
  73.   
  74.     $stmt->execute(); 
  75.   
  76.     $stmt->bindcolumn('username',$username); 
  77.     $stmt->bindcolumn('password',$password); 
  78.   
  79.     while($stmt->fetch()){ 
  80.         echo $username; 
  81.         echo $password; 
  82.         echo '<br>'; 
  83.     } 
  84.  */  
  85.    
  86. }catch(PDOException $e){  
  87.     echo $e->getMessage();  
  88. }  
  89. ?>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值