一. 数据库操作

1. 查询所有记录

  1. select * from tableName;

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

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

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

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

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

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

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

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

方式一

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

方式二

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

4. 条件查询

select * from tableName where condition;

where 关键字注意:

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

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

MySQL 比较运算符:

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

MySQL 逻辑运算符:

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

5. 模糊查询

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

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

_ 表示匹配一个任意字符

%表示匹配多个任意字符

6. 空值查询

select * from tableName where field is null;
select * from tableName where field is not null;

二. PDO操作

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

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

1. 执行SQL查询语句

PDOStatement PDO::query(String sql)
<?php
header("content-type:text/html;charset=utf-8");

$url = "mysql:host=127.0.0.1;dbname=cai";
$user = "root";
$pwd = "123";

$conn = new PDO($url,$user,$pwd);
$st = $conn->query("select * from user");

print_r($st);

2. 获取查询结果

获取结果集中的一条记录

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开始的列号的数组
$res = $st->fetch(PDO::FETCH_ASSOC);

3. 练习(文章展示)

<?php
    $url = "mysql:host=mysql;dbname=database_lesson_30511_14_11193";//数据库ip和库名
    $user = "lesson_30511_14_11193";//数据库用户
    $pwd = "73dfdd838bac3292c8fef675e72631de";//数据库密码

    $pdo = new PDO($url,$user,$pwd);
    // DML inert deletet update
    // DQL select
    // $sql = "select * from content;";
    // $sql = "select * from content where id = 1;";
    // $sql = "select * from content where source is not null;";
    // $sql = "select * from content where name like '%张一山%';";

    $sql = "select name as title,content,source,author,click,date  from content where id = 1";

    echo $sql . "<br>";
    $st = $pdo->query($sql);

    $res = $st->fetch(PDO::FETCH_ASSOC);//



    $name = $res['name'];
    $content = $res['content'];
    $source = $res['source'];
    $author = $res['author'];
    $click = $res['click'];
    $date = $res['date'];
    // echo $content;

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #news{
            width:1200px;
            margin: 0 auto;
            text-align: center;
            background: rgb(245, 248, 253);
        }
        #title{
            list-style: none;
            display: flex;
            justify-content: space-around;
        }
        #content{
            text-align: left;
        }
        h1{
            color:red;
        }
    </style>
</head>
<body>
    <div id="news">
        <h1> <?php echo $name; ?></h1>
        <ul id="title">
            <li>来源: <?php echo $source; ?></li>
            <li>作者: <?php echo $author; ?></li>
            <li>发布时间 : <?php echo $date; ?></li>
            <li> <?php echo $click; ?> 次浏览</li>
        </ul>
        <div id="content"> <?php echo $content; ?></div>
    </div>

</body>
</html>