Blogger Information
Blog 9
fans 0
comment 0
visits 7587
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP操作数据库
金牌马甲
Original
716 people have browsed it

1.连接数据库

1.1配置文件

代码展示:
  1. <?php
  2. //连接参数
  3. return [
  4. //类型
  5. 'type' => $type ?? 'mysql',
  6. //默认主机名
  7. 'host' => $username ?? 'localhost',
  8. //默认编码集
  9. 'charset' => $charset ?? 'utf8',
  10. //默认端口号
  11. 'port' => $porst ?? '3306',
  12. //默认用户名
  13. 'username' => $usernam ?? 'root',
  14. //默认用户密码
  15. 'password' => $password ?? 'root',
  16. //默认数据库
  17. 'dbname' => $dbname ?? 'phpedu',
  18. ];
  19. ?>

1.2连接数据库

代码展示:
  1. <?php
  2. //导入配置文件
  3. $config = require 'config.php';
  4. //将参数数组分解成独立的变量
  5. extract($config);
  6. //1.创建数据库的连接对象,连接数据库
  7. $mysqli = new mysqli($host, $username, $password, $dbname);
  8. //2.检测错误,die/exit, 终止代码,后面不再执行
  9. if ($mysqli->connect_errno) die ('Connect Error:'. $mysqli->connect_error);
  10. //3.设置字符编码
  11. $mysqli-> set_charset($charset);
  12. ?>

2.PHP操作数据库的增、删、改、查

2.1 INSERT

2.1.1新增单条记录
代码展示:
  1. <?php
  2. //连接数据库
  3. require 'connect.php';
  4. //操作数据表
  5. $sql = 'INSERT `users` SET `name`=?, `email`=?, `password`=?;';
  6. //预处理操作防止sql注入攻击
  7. $stmt = $mysqli->prepare($sql);
  8. //占位符绑定变量名
  9. $stmt->bind_param('sss', $name, $email, $password);
  10. $name = 'mike';
  11. $email = 'mike@php.cn';
  12. $password = sha1('456789');
  13. //执行sql语句
  14. $stmt->execute() or die($stmt->error);
  15. if($stmt->affected_rows === 1)
  16. printf('成功新增 %s 条记录,新增主键id是: %d', $stmt->affected_rows, $stmt->insert_id);
  17. else echo '没有记录被添加';
  18. $mysqli->close();
  19. ?>
运行结果:

2.1.2新增多条记录
代码展示:
  1. <?php
  2. require 'connect.php';
  3. $sql = 'INSERT `users` SET `name`=?, `email`=?, `password`=?;';
  4. $stmt = $mysqli->prepare($sql);
  5. $stmt->bind_param('sss', $name, $email, $password);
  6. $users = [
  7. ['name'=>'james', 'email'=>'james@php.cn', 'password'=>sha1('123789')],
  8. ['name'=>'jhon', 'email'=>'jhon@php.cn', 'password'=>sha1('123789')],
  9. ['name'=>'jacky', 'email'=>'jacky@php.cn', 'password'=>sha1('123789')],
  10. ];
  11. foreach($users as $user){
  12. extract($user);
  13. if ($stmt->execute())
  14. printf('成功新增 %s 条记录,新增主键id是:%d <br>', $stmt->affected_rows, $stmt->insert_id);
  15. else
  16. exit(sprintf('更新失败, $d, $s', $stmt->errno, $stmt->error));
  17. }
  18. $mysqli->close();
  19. ?>
运行结果:

2.2 UPDATE

代码展示:
  1. <?php
  2. require 'connect.php';
  3. $sql = 'UPDATE `users` SET `name`=?, `email`=?, `password`=? WHERE `id`=?;';
  4. $stmt = $mysqli->prepare($sql);
  5. $stmt->bind_param('sssi', $name, $email, $password, $id);
  6. $user = ['name'=>'ododo', 'email'=>'ododo@php.cn', 'password'=>'123321', 'id'=>1];
  7. extract($user);
  8. $stmt->execute() or die($stmt->error);
  9. if ($stmt->affected_rows === 1)
  10. printf('成功更新 %s 条记录', $stmt->affected_rows);
  11. else echo '没有记录被更新';
  12. $mysqli->close();
  13. ?>
运行结果:

2.3 DELETE

代码展示:
  1. <?php
  2. require 'connect.php';
  3. $sql = 'DELETE FROM `users` WHERE `id`=?;';
  4. $stmt = $mysqli->prepare($sql);
  5. $stmt->bind_param('i', $id);
  6. $id = 1;
  7. $stmt->execute() or die($stmt->error);
  8. if ($stmt->affected_rows === 1)
  9. printf('成功删除 %s 条记录', $stmt->affected_rows);
  10. else echo '没有记录被删除';
  11. $mysqli->close();
  12. ?>
运行结果:

2.4 SELECT

2.4.1 逐条查询
  1. fetch()_assoc() + while()
代码展示:
  1. <?php
  2. require 'connect.php';
  3. $sql = 'SELECT `id`, `name`, `email` FROM `users` WHERE `id`>?;';
  4. $stmt = $mysqli->prepare($sql);
  5. $stmt->bind_param('i', $id);
  6. $id = 17;
  7. $stmt->execute() or die($stmt->error);
  8. $result = $stmt->get_result();
  9. if ($result->num_rows === 0) exit('结果集为空');
  10. while ($user = $result->fetch_assoc()){
  11. vprintf('%d: %s | %s <br>', $user);
  12. }
  13. $result->free();
  14. $mysqli->close();
  15. ?>
运行结果:

2.4.2 一次性获取所有满足条件的记录
  1. fetch_all(), foreach()
代码展示:
  1. <?php
  2. require 'connect.php';
  3. $sql = 'SELECT `id`, `name`, `email` FROM `users` WHERE `id`>?;';
  4. $stmt = $mysqli->prepare($sql);
  5. $stmt->bind_param('i', $id);
  6. $id = 28;
  7. $stmt->execute() or die($stmt->error);
  8. $result = $stmt->get_result();
  9. if ($result->num_rows === 0) exit('结果集为空');
  10. $users = $result->fetch_all(MYSQLI_ASSOC);
  11. foreach($users as $user){
  12. vprintf('%d: %s | %s <br>', $user);
  13. }
  14. $result->free();
  15. $mysqli->close();
  16. ?>
运行结果:

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