MySQL增:insert into 表名 (字段1,...,字段n) values (值1,...,值n)
MySQL删:delete from 表名 where 条件
MySQL改:update 表名 set 键1=值1,...,键n=值n where 条件
MySQL查:select * from 表名 where 条件 order by 字段 asc|desc limit 起始位置,查询条数
连接数据库
$pdo = new PDO(‘mysql:dbname=web‘, ‘root‘, ‘root‘);
返回的数组类型
// 返回一维数组 $data = $pdoStatement->fetch(PDO::FETCH_ASSOC);
// 返回二维数组 $datas = $pdoStatement->fetchAll(PDO::FETCH_ASSOC);
查看执行完数据库语句后的情况
var_dump($rs);
一 增
-
增 固定值 $rs = $pdo->exec("insert into joke (title,content,author, created_at, updated_at) values (‘标题‘, ‘内容‘, ‘作者‘, ‘2017-9-9 11:12:12‘, ‘2017-9-9 11:12:12‘) ");
-
增 变量 $rs = $pdo->exec("insert into joke (title,content,author, created_at, updated_at) values (‘{$title}‘, ‘{$content}‘, ‘{$author}‘, ‘{$createdAt}‘, ‘{$updatedAt}‘)");
二 删
-
删 固定值 $rs = $pdo->exec("delete from joke where id = 1");
-
删 变量 $rs = $pdo->exec("delete from joke where id = {$id}");
三 改
-
改 固定值 $rs = $pdo->exec("update joke set title=‘神龙教主‘ where id = 3");
-
改 变量 $rs = $pdo->exec("update joke set title=‘{$title}‘ where id = {$id}");
四 查
-
查询 select * from stu where uname = "张三" select * from stu where uname like ‘%fa%‘ 含fa select * from stu where uname like ‘fa%‘ 开头fa select * from stu where uname like ‘%fa‘ fa结尾
-
准确查询 $uname = @$_GET[‘uname‘]; $pdoStatement = $pdo->query("select * from stu where uname = ‘{$uname}‘;");
-
模糊查询 $pdoStatement = $pdo->query("select * from stu where uname like ‘%{$search}%‘;");
-
排序分页查询 select * from 表名 order by id asc limit 起始位置,页面显示条数 $pagesize = 2; $start = ($pageno - 1) * $pagesize; $pdoStatement = $pdo->query("select * from stu order by id asc limit {$start},{$pagesize}");
-
查询总条数
-