一. 数据库操作

1. 查询所有记录

  1. select * from tableName;

通过 select 语句,查询 user 表中所有记录。

select:代表当前是查询语句。

**代表查询 user 表中的所有字段。

from:用于指定将要查询的表名。

user:代表查询 user 表的数据。

2. 查询记录中指定字段的数据

  1. select field1, field2,.... from tablename;

3. 查询指定数据表中指定字段中的数据,并为显示字段起别名

方式一

  1. select field1 as aliasName, field2 as aliasName,.... from tablename;

方式二

  1. select field1 aliasName, field2 aliasName,.... from tablename;

4. 条件查询

  1. select * from tableName where condition;

where 关键字注意:

1、where 关键字可以使用比较运算符指定任何条件。

2、where 条件可以是一个,也可以是多个,这些条件可以用逻辑运算符连接。

MySQL 比较运算符:

比较运算符 说明
= 等于,检测两个值是否相等,如果相等则返回true
<>、!= 不等于,检测两个值是否不相等,如果不相等则返回true
< 小于,检测左边的值是否小于右边的值,如果小于则返回true
<= 小于或等于,检测左边的值是否小于或等于右边的值,如果小于或等于则返回true
> 大于,检测左边的值是否大于右边的值,如果大于则返回true
>= 大于或等于,检测左边的值是否大于或等于右边的值,如果大于或等于则返回true

MySQL 逻辑运算符:

逻辑运算符 说明
and 表示多个条件都必须满足
or 表示满足任意条件就可以
not 表示除了满足条件以外的数据

5. 模糊查询

  1. select * from tableName where field like '_%';

模糊查询用于查询对字符串类型数据进行部分内容的匹配查询

_ 表示匹配一个任意字符

%表示匹配多个任意字符

6. 空值查询

  1. select * from tableName where field is null;
  2. select * from tableName where field is not null;

二. PDO操作

在 PHP 中,想要对 MySQL 执行查询操作,可以通过 PDO 的 query() 方法来实现。

query() 方法不同于 exec() 方法,它通常用于执行 select 语句,返回值是 PDOStatement 实例。

1. 执行SQL查询语句

  1. PDOStatement PDO::query(String sql)
  1. <?php
  2. header("content-type:text/html;charset=utf-8");
  3. $url = "mysql:host=127.0.0.1;dbname=cai";
  4. $user = "root";
  5. $pwd = "123";
  6. $conn = new PDO($url,$user,$pwd);
  7. $st = $conn->query("select * from user");
  8. print_r($st);

2. 获取查询结果

获取结果集中的一条记录

  1. array PDOStatement::fetch( [ int $fetch_style [ , int $cursor_orientation [ , int $cursor_offset ]]])

通过 PDOStatement 实例,调用 fetch() 方法,获得查询结果集中的一条记录。

fetch_style 参数决定 POD 如何返回行,此值必须是 PDO::FETCH_* 系列常量中的一个,缺省为 PDO::ATTR_DEFAULT_FETCH_MODE 的值 (默认为 PDO::FETCH_BOTH )。

  • PDO::FETCH_ASSOC:返回一个索引为结果集列名的数组(常用)
  • PDO::FETCH_BOTH(默认):返回一个索引为结果集列名和以0开始的列号的数组
  1. $res = $st->fetch(PDO::FETCH_ASSOC);

3. 练习(文章展示)

  1. <?php
  2. $url = "mysql:host=mysql;dbname=database_lesson_30511_14_11193";//数据库ip和库名
  3. $user = "lesson_30511_14_11193";//数据库用户
  4. $pwd = "73dfdd838bac3292c8fef675e72631de";//数据库密码
  5. $pdo = new PDO($url,$user,$pwd);
  6. // DML inert deletet update
  7. // DQL select
  8. // $sql = "select * from content;";
  9. // $sql = "select * from content where id = 1;";
  10. // $sql = "select * from content where source is not null;";
  11. // $sql = "select * from content where name like '%张一山%';";
  12. $sql = "select name as title,content,source,author,click,date from content where id = 1";
  13. echo $sql . "<br>";
  14. $st = $pdo->query($sql);
  15. $res = $st->fetch(PDO::FETCH_ASSOC);//
  16. $name = $res['name'];
  17. $content = $res['content'];
  18. $source = $res['source'];
  19. $author = $res['author'];
  20. $click = $res['click'];
  21. $date = $res['date'];
  22. // echo $content;
  23. ?>
  24. <!DOCTYPE html>
  25. <html lang="en">
  26. <head>
  27. <meta charset="UTF-8">
  28. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  29. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  30. <title>Document</title>
  31. <style>
  32. #news{
  33. width:1200px;
  34. margin: 0 auto;
  35. text-align: center;
  36. background: rgb(245, 248, 253);
  37. }
  38. #title{
  39. list-style: none;
  40. display: flex;
  41. justify-content: space-around;
  42. }
  43. #content{
  44. text-align: left;
  45. }
  46. h1{
  47. color:red;
  48. }
  49. </style>
  50. </head>
  51. <body>
  52. <div id="news">
  53. <h1> <?php echo $name; ?></h1>
  54. <ul id="title">
  55. <li>来源: <?php echo $source; ?></li>
  56. <li>作者: <?php echo $author; ?></li>
  57. <li>发布时间 : <?php echo $date; ?></li>
  58. <li> <?php echo $click; ?> 次浏览</li>
  59. </ul>
  60. <div id="content"> <?php echo $content; ?></div>
  61. </div>
  62. </body>
  63. </html>