Blogger Information
Blog 26
fans 0
comment 0
visits 21459
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
用MySQLI 操作
default
Original
658 people have browsed it

数据库连接

  1. namespace mysql_edu;
  2. use mysqli;
  3. $config = require __DIR__ . '/databases.php';
  4. //检测是否是个合法的数组
  5. //print_r(extract($config));
  6. extract($config);
  7. //$mysqli = new mysqli($host,$username,$password,$dbname);
  8. $mysqli = new mysqli($host,$username,$password,$dbname);
  9. //print_r($mysqli);
  10. if ($mysqli->connect_errno)echo $mysqli->connect_error;
  11. //设置mysql的字符集
  12. $mysqli->set_charset($charset);

添加

  1. namespace mysql_edu;
  2. require 'connect.php';
  3. $arr='刘云111';
  4. //多条添加
  5. //array_walk($arr, function(&$item, $key, $length) {
  6. // if ($key < $length-1 ) $item = "'$item'";
  7. //}, count($arr));
  8. //
  9. //echo $data = implode(', ', $arr);die();
  10. //$sql = "INSERT `staff` (`name`) VALUES ($arr)";
  11. $sql = "INSERT `staff` (`name`) VALUES ('$arr')";
  12. //echo $sql;die();
  13. //$sql = "INSERT `staffs` (`name`,`age`,`sex`,`position`,`mobile`,`hiredate`) VALUES ($data)";
  14. if ($mysqli->query($sql)) {
  15. if ($mysqli->affected_rows > 0) {
  16. echo '成功添加了 ' . $mysqli->affected_rows . ' 条记录, 新增记录主键ID: ' . $mysqli->insert_id;
  17. } else {
  18. echo '没有添加新记录';
  19. }
  20. } else {
  21. die('添加失败'. $mysqli->errno . ' : ' . $mysqli->error);
  22. }
  23. $mysqli->close();

单条查询

  1. namespace mysql_edu;
  2. require 'connect.php';
  3. $sql = "SELECT `name` FROM `staff`";
  4. //echo $sql;
  5. $mysqli_result = $mysqli->query($sql);
  6. // 指针复位
  7. $mysqli_result->data_seek(0);
  8. if ($mysqli_result && $mysqli_result->num_rows > 0 ) {
  9. while ($staff = $mysqli_result->fetch_assoc()) {
  10. printf('<pre>%s</pre>', print_r($staff, true));
  11. }
  12. } else {
  13. echo '查询失败';
  14. }
  15. //$mysqli_result->free_result();
  16. $mysqli->close();

多条查询

  1. namespace mysql_edu;
  2. require 'connect.php';
  3. $sql = "SELECT `name` FROM `staff`";
  4. //echo $sql;
  5. $mysqli_result = $mysqli->query($sql);
  6. if ($mysqli_result && $mysqli_result->num_rows > 0 ) {
  7. $staffs = $mysqli_result->fetch_all();
  8. foreach ($staffs as $staff) {
  9. // vprintf() 与 printf()功能一样, 区别 就是参数是数组
  10. vprintf('<li> 姓名: %s</li>', $staff);
  11. }
  12. } else {
  13. echo '查询失败或没有查询到满足条件的员工';
  14. }
  15. //$mysqli_result->free_result();
  16. $mysqli->close();

更新

  1. <?php
  2. namespace pdo_edu;
  3. require 'connect.php';
  4. $arr=['name'=>'志佳'];
  5. array_walk($arr, function(&$item, $key) {
  6. $item = "`$key` = '$item'";
  7. });
  8. $data = implode(', ', $arr);
  9. $sql= "UPDATE `staff` SET ". $data."WHERE `id` = 14";
  10. if ($mysqli->query($sql)) {
  11. if ($mysqli->affected_rows > 0) {
  12. echo '成功更新了 ' . $mysqli->affected_rows . ' 条记录';
  13. } else {
  14. echo '没有更新任何记录';
  15. }
  16. } else {
  17. die('更新失败'. $mysqli->errno . ' : ' . $mysqli->error);
  18. }
  19. $mysqli->close();

删除

  1. namespace mysql_edu;
  2. require 'connect.php';
  3. $sql="DELETE FROM `staff` WHERE `id`=".$_GET['id'];
  4. if ($mysqli->query($sql)) {
  5. if ($mysqli->affected_rows > 0) {
  6. echo '成功删除了 id=' .$_GET['id'] . ' 的记录';
  7. } else {
  8. echo '没有删除任何记录';
  9. }
  10. } else {
  11. die('删除失败'. $mysqli->errno . ' : ' . $mysqli->error);
  12. }
  13. // 3. 关闭连接
  14. $mysqli->close();
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:你可能会觉得mysqli比较麻烦, 其实这才是最规范的操作步骤, 不是吗?
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