Blogger Information
Blog 29
fans 0
comment 0
visits 18386
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
pdo操作
CC
Original
755 people have browsed it

pdo开启与连接

  • 查看php配置(检测pdo是否开启)
    1. <?php
    2. phpinfo();

    pdo连接模板

  • conflg部分
  1. <?php
  2. // 数据库的配置参数
  3. return [
  4. 'type' => 'mysql',
  5. 'host' => '127.0.0.1',
  6. 'dbname' => 'phpedu',
  7. 'port' => '3306',
  8. 'charset' => 'utf8mb4',
  9. 'username' => 'root',
  10. 'password' => 'root',
  11. ];
  • connect部分
  1. <?php
  2. // 导入配置文件
  3. $config = require __DIR__ . '/config.php';
  4. //extract类似list函数 将关联数组成员解析成独立变量
  5. extract($config);
  6. $dsn = sprintf('%s:dbname=%s;',$type,$dbname);
  7. try {
  8. // pdo构造方法,实例
  9. $pdo =new PDO($dsn, $username, $password);
  10. // $pdo =new PDO('mysql:dbname=phpedu','root','root');
  11. // 设置结果集的返回类型
  12. $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  13. // var_dump($pdo,'连接成功');
  14. } catch (PDOException $e) {
  15. die( '连接失败:' . $e->getMessage());
  16. }
  17. `

pdo查询

  1. <?php
  2. // 1.连接数据库
  3. use PhpMyAdmin\Sql;
  4. require __DIR__.'/connect.php';
  5. // 2.执行操作(预处理)
  6. // sql语句注意事项:语句全大写,表名和反引号加反引号,?匿名占位符
  7. $sql = 'SELECT* FROM `staffs` WHERE `salary` BETWEEN ? AND ?;';
  8. $stmt = $pdo->prepare($sql);
  9. // var_dump($stmt);
  10. // 预处理语句,5000和8000对应sql语句?
  11. $stmt->execute([5000,8000]);
  12. // $stmt->debugDumpParams();
  13. // $row=$stmt->fetch();
  14. // print_r($row);
  15. // $rows =[];
  16. // // 结果集保存在stmt,fetch方法只能输出一个
  17. // while($row=$stmt->fetch()){
  18. // $rows[]=$row;
  19. // }
  20. // foreach($rows as $row){
  21. // echo print_r($row,true);'<br>';
  22. // }
  23. $rows=$stmt->fetchAll();
  24. foreach($rows as $row){
  25. echo print_r(array_slice($row,0,5),true),'<br>';
  26. }

分页查询

  • bindParam方法绑定
  1. <?php
  2. // 1.连接数据库
  3. use PhpMyAdmin\Sql;
  4. require __DIR__ . '/connect.php';
  5. // 2.执行操作(预处理)
  6. // sql语句注意事项:语句全大写,表名和反引号加反引号,?匿名占位符
  7. $sql = 'SELECT* FROM `staffs` WHERE `salary` BETWEEN ? AND ? LIMIT ?;';
  8. $stmt = $pdo->prepare($sql);
  9. // var_dump($stmt);
  10. // 预处理语句,5000和8000,5对应sql语句?
  11. $start = 5000;
  12. $end=8000;
  13. $num=5;
  14. // bindParam方法绑定到sql(参数位置,变量名,输出类型)
  15. $stmt->bindParam(1,$start,PDO::PARAM_INT);
  16. $stmt->bindParam(2, $end, PDO::PARAM_INT);
  17. $stmt->bindParam(3, $num, PDO::PARAM_INT);
  18. $stmt->execute();
  19. $rows = $stmt->fetchAll();
  20. foreach ($rows as $row) {
  21. echo print_r(array_slice($row, 0, 5), true), '<br>';
  22. }
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post