Blogger Information
Blog 39
fans 0
comment 0
visits 30670
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP:再试mysqli的CURD
Original
669 people have browsed it

mysqli接口的数据库连接

  1. <?php
  2. // 数据库连接
  3. namespace mysql_edu;
  4. use mysqli;
  5. // 1.加载参数
  6. $config = require __DIR__.'/config/database-exe.php';
  7. // 将关联数组中的键值对, 转为变量的名值对
  8. extract($config);
  9. // 2.连接数据库
  10. $mysqli = new mysqli($host,$username,$password,$dbname);
  11. // 3.判断连接是否成功
  12. if($mysqli->connect_errno){
  13. echo $mysqli->connect_error;}else{
  14. echo '数据库连接成功!';
  15. }
  16. var_dump($mysqli);

实例效果

查询单条数据

  1. <?php
  2. require 'connect-exe.php';
  3. $sql = "SELECT * FROM `table01` where `age`>30 ";
  4. $mysqli_result = $mysqli->query($sql);
  5. // 指针复位
  6. $mysqli_result->data_seek(0);
  7. // 查询结果
  8. echo $mysqli_result->num_rows;
  9. if($mysqli_result && $mysqli_result->num_rows >0){
  10. while($hero = $mysqli_result->fetch_assoc())
  11. {
  12. printf('<pre>%s</pre>',print_r($hero,true));
  13. }
  14. }else{
  15. echo '查讯失败!';
  16. }
  17. // 释放结果集
  18. $mysqli_result->free_result();
  19. $mysqli->close();

实例效果

查询多条数据

  1. <?php
  2. require 'connect-exe.php';
  3. $sql = "SELECT * FROM `table01` where `age`>20";
  4. $mysqli_result = $mysqli->query($sql);
  5. $heros = $mysqli_result->fetch_all(MYSQLI_ASSOC);
  6. printf('<pre>%s</pre>',print_r($heros,true));
  7. $mysqli_result->free_result();
  8. $mysqli->close();

实例效果

新增数据

  1. <?php
  2. // 新增数据
  3. require 'connect-exe.php';
  4. $sql = "insert into `table01` (`name`,`skill`,`age`) values ('wcy','jyzj',68)";
  5. $mysqli->query($sql);
  6. if($mysqli->query($sql)){
  7. echo '成功添加了'.$mysqli->affected_rows.'条数据!';
  8. }else{
  9. echo '添加失败!'.$mysqli->errno.$mysqli->error;
  10. }
  11. $mysqli->close();

实例效果

更新数据

  1. <?php
  2. // 更新数据
  3. require 'connect-exe.php';
  4. $sql = "UPDATE `TABLE01` SET `name`='王重阳',`skill`='九阴真经',`age`=68 WHERE `id`=13 ";
  5. $mysqli->query($sql);
  6. if($mysqli->query($sql)){
  7. if($mysqli->affected_rows>0){
  8. echo '成功更新了'.$mysqli->affected_rows.'条数据!';
  9. }else{
  10. echo '成功更新0条';
  11. }
  12. }else{
  13. echo '数据更新失败!'.$mysqli->error;
  14. }
  15. $mysqli->close();

实例效果

删除数据

  1. <?php
  2. namespace mysql_edu;
  3. require 'connect-exe.php';
  4. $sql = "DELETE FROM `table01` WHERE `id`=16";
  5. if( $mysqli->query($sql)){
  6. if($mysqli->affected_rows>0){
  7. echo '删除了'.$mysqli->affected_rows.'条数据!';
  8. }else{
  9. echo '没有删除记录!';
  10. }
  11. }else{
  12. echo '删除失败!';
  13. }
  14. $mysqli->close();

实例效果

总结:
1.于PDO接口相比,连接参数相同,步骤一样。
2.具体的查询语句不同。
3.明明更新了1条,affected_rows返回0?数据库已更新啊!
4.刚刚接触不容易记住,先记些骨干吧。

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