Blogger Information
Blog 26
fans 0
comment 0
visits 21525
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数据库基础与常用操作
default
Original
674 people have browsed it

数据库配置

  1. //数据库连接方式
  2. return[
  3. // 数据库连接类型
  4. 'type'=>$type ??'mysql',
  5. // 数据库默认主机
  6. 'host'=>$host ??'localhost',
  7. // 数据库名字
  8. 'dbname'=>$dbname ??'phpedu',
  9. // 数据库字符集
  10. 'charset'=>$charset ??'utf8',
  11. // 数据库端口号
  12. 'port'=>$port ??'3306',
  13. // 数据库账号
  14. 'username'=>$username ??'root',
  15. // 数据库密码
  16. 'password'=>$password ??'root',
  17. ];

连接数据库

  1. namespace pdo_edu;
  2. use PDO;
  3. use Exception;
  4. $config = require 'config/databases.php';
  5. $type=$config['type'];
  6. $host=$config['host'];
  7. $dbname=$config['dbname'];
  8. $username=$config['username'];
  9. $password=$config['password'];
  10. //创建DSN
  11. $dsn=sprintf('%s:host=%s;dbname=%s',$type,$host,$dbname);
  12. //echo $dsn;
  13. try{
  14. $pdo=new PDO($dsn,$username,$password);
  15. // var_dump($pdo);
  16. }catch ( Exception $e){
  17. die($e->getMessage());
  18. }

增删改查

增加

  1. namespace pdo_edu;
  2. use PDO;
  3. require 'pdo.php';
  4. $sql="INSERT `staff` SET `name` =?";
  5. $stmt=$pdo->prepare($sql);
  6. $data=['王壮'];
  7. $stmt->execute($data);
  8. //判断是否执行成功
  9. if ($stmt->rowCount()===1){
  10. echo '新增主键成功,';
  11. }else{
  12. echo '新增失败';
  13. print_r($stmt->errorInfo());
  14. }
  15. unset($pdo);

删除

  1. namespace pdo_edu;
  2. require 'pdo.php';
  3. $sql = "DELETE FROM `staff` WHERE `id`=:id";
  4. $stmt=$pdo->prepare($sql);
  5. $id=filter_input(INPUT_GET,'id',FILTER_VALIDATE_INT);
  6. $stmt->execute(['id'=>$_GET['id']]);
  7. if ($stmt->rowCount()===1){
  8. echo '删除成功';
  9. }

修改

  1. namespace pdo_edu;
  2. require 'pdo.php';
  3. $sql = "UPDATE `staff` SET `name` = ? WHERE `id`=?";
  4. $stmt=$pdo->prepare($sql);
  5. $stmt->execute(['王壮好样的',3]);
  6. if ($stmt->rowCount()===1){
  7. echo '更新成功';
  8. }else{
  9. echo '没有更新成功';
  10. print_r($stmt->errorInfo());
  11. }
  12. unset($pdo);

查询

  1. namespace pdo_edu;
  2. require 'pdo.php';
  3. use PDO;
  4. $sql = 'SELECT * FROM `staff`';
  5. //预处理对象$stmt防止 SQl注入
  6. $stmt=$pdo->prepare($sql);
  7. //使用预处理对象调用 execute()执行这条语句
  8. $stmt->execute();
  9. //echo $staff=$stmt->fetch(PDO::FETCH_ASSOC);
  10. //while ($staff = $stmt->fetch(PDO::FETCH_ASSOC)) {
  11. // printf('<pre>%s</pre>',print_r($staff,true));
  12. //}
  13. $staff=$stmt->fetchall(PDO::FETCH_ASSOC);
  14. foreach ($staff as $staf){
  15. printf('id=%s:姓名=%s<br>', $staf['id'], $staf['name']);
  16. }
  17. unset($pdo);
Correcting teacher:天蓬老师天蓬老师

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