Blogger Information
Blog 47
fans 0
comment 0
visits 21056
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP连接数据库操作
P粉036614676
Original
666 people have browsed it

1.连接数据库操作

  1. PDO:是类的接口,实例化后就可以使用该类的接口了
  2. $db = new PDO('mysql:host=localhost;dbname=phpedu','root','901026yk');

2.插入操作

1.索引插入

  1. <?php
  2. namespace pdo_edu;
  3. use PDO;
  4. $db = new PDO('mysql:host=localhost;dbname=phpedu','root','901026yk');
  5. $sql = 'insert into text values (?,?,?);';
  6. $stmt = $db->prepare($sql);
  7. $data = [2,1,'yn'];
  8. $stmt->execute($data);

2.关联插入

  1. <?php
  2. namespace pdo_edu;
  3. use PDO;
  4. $db = new PDO('mysql:host=localhost;dbname=phpedu','root','901026yk');
  5. $sql = 'insert into text values (:id,:sex,:name);';
  6. $stmt = $db->prepare($sql);
  7. $data = ["id" => 3,"sex" => 1,"name" => '杨康'];
  8. $stmt->execute($data);

3.插入指定类型

直接插入都是字符类型,使用bindValue和bindParame()可以指定类型

  1. <?php
  2. namespace pdo_edu;
  3. use PDO;
  4. $db = new PDO('mysql:host=localhost;dbname=phpedu','root','901026yk');
  5. $sql = 'insert into text values (?,?,?);';
  6. $stmt = $db->prepare($sql);
  7. list($id,$sex,$name) = [4,1,'杨娜'];
  8. $stmt->bindValue(1,$id,PDO::PARAM_INT);
  9. $stmt->bindValue(2,$sex,PDO::PARAM_INT);
  10. $stmt->bindValue(3,$name);
  11. $stmt->execute();
  1. <?php
  2. namespace pdo_edu;
  3. /**
  4. * $stmt->rowCount()
  5. * $stmt->errorInfo()
  6. */
  7. use PDO;
  8. $db = new PDO('mysql:host=localhost;dbname=phpedu','root','901026yk');
  9. $sql = "insert into text values (?,?,?)";
  10. $stmt = $db->prepare($sql);
  11. $stmt->bindParam(1,$id,PDO::PARAM_INT);
  12. $stmt->bindParam(2,$sex,PDO::PARAM_INT);
  13. $stmt->bindParam(3,$name,PDO::PARAM_STR);
  14. $data = [
  15. [7,1,'wusong'],
  16. [8,0,'banjinl'],
  17. [9,1,'wudal'],
  18. ];
  19. foreach ($data as list($id,$sex,$name))
  20. {
  21. try {
  22. $stmt->execute();
  23. }catch (\PDOException $e)
  24. {
  25. print_r($e);
  26. }
  27. }

3.跟新和删除操作

  1. <?php
  2. namespace pdo_edu;
  3. /**
  4. * $stmt->rowCount()
  5. * $stmt->errorInfo()
  6. */
  7. use PDO;
  8. use PDOException;
  9. $db = new PDO('mysql:host=localhost;dbname=phpedu', 'root', '901026yk');
  10. //$sql = <<<SQL
  11. //update text set sex = ? where id = ?;
  12. //SQL;
  13. //if(!stripos($sql,'where'))
  14. //{
  15. // exit('SQL语句没有where');
  16. //}
  17. $sql = <<<SQL
  18. delete from text where id = ?;
  19. SQL;
  20. $stmt = $db->prepare($sql);
  21. $data = [8];
  22. try {
  23. $stmt->execute($data);
  24. }catch (PDOException $e)
  25. {
  26. echo '错误信息:' . $e;
  27. }

4.取操作

1.fetch() + while()

  1. <?php
  2. /**
  3. * fetch() 和 while()
  4. */
  5. namespace pdo_edu;
  6. /**
  7. * $stmt->rowCount()
  8. * $stmt->errorInfo()
  9. */
  10. use PDO;
  11. use PDOException;
  12. $db = new PDO('mysql:host=localhost;dbname=phpedu', 'root', '901026yk');
  13. $sql = <<<SQL
  14. select * from text limit ?,?;
  15. SQL;
  16. $stmt = $db->prepare($sql);
  17. //$data = [1,3];// $stmt->execute($data);执行的$data的两个参数都是字符
  18. $stmt->bindValue(1,1,PDO::PARAM_INT);
  19. $stmt->bindValue(2,3,PDO::PARAM_INT);
  20. try {
  21. $stmt->execute();
  22. $staff = $stmt->fetch(PDO::FETCH_ASSOC);
  23. while($staff)
  24. {
  25. printf('<pre>%s</pre>', print_r($staff, true));
  26. $staff = $stmt->fetch(PDO::FETCH_ASSOC);
  27. }
  28. } catch (PDOException $e) {
  29. echo '错误信息:' . $e;
  30. }

2.fetchAll + foreach()

  1. <?php
  2. /**
  3. * fetchAll() 和 foreach()
  4. */
  5. namespace pdo_edu;
  6. use PDO;
  7. use PDOException;
  8. $db = new PDO('mysql:host=localhost;dbname=phpedu', 'root', '901026yk');
  9. $sql = <<<SQL
  10. select * from text limit ?,?;
  11. SQL;
  12. $stmt = $db->prepare($sql);
  13. $stmt->bindValue(1,1,PDO::PARAM_INT);
  14. $stmt->bindValue(2,3,PDO::PARAM_INT);
  15. try {
  16. $stmt->execute();
  17. $staffs = $stmt->fetchAll(PDO::FETCH_ASSOC);
  18. foreach ($staffs as $staff)
  19. {
  20. print_r($staff);
  21. }
  22. } catch (PDOException $e) {
  23. echo '错误信息:' . $e;
  24. }
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:注意二级标题的错别字
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