Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
注意:字符串要加'英文单引号,int数字类型可以不加,表明和字段名加`反引号(键盘1的左边),是特殊名称必须加`号,不是可以不加。
insert into `baoming`(`mingzi`,`tel`) values('太阳',13011112222); //插入orders表tel字段的值
INSERT INTO baoming VALUES(99,'admin99',null); //从左到右每个字段都要插入数据,不能多写字段数据,也不能少写字段数据,如果某个字段要为空就用null代替。
INSERT INTO baoming set name='太阳',tele=18900008888; //这种插入更方便,适合插入字段有很多,不会很乱。
select count(*) from orders; //查询表有多少条数据
select max(tel) from orders; //查询表tel字段最大数值
select min(tel) from orders; //查询表tel字段最小数值
select a.orderid,b.orderid from orders as a left join orders_detail as b on a.orderid=b.orderid where id=55; //左表orders连接orders_detail
右表,显示在一起
select * from orders limit 10; //查询10条数据,或者limit 10,20(10到20条)
select * from orders order by id desc; //降序查询
select tel from orders where tel like `%6%`; //模糊查找数据有6的值,%相当于所有数据
SELECT * from baoming where name LIKE 'o_'; //查找o开始的数据,_相当于一个数据,后面有多少写多少,写少或者写多都不能查找到数据(不建议用)
select * from orders where `phone` is null; //phone字段为空的数据
select * from orders where `phone` is not null; //phone字段不为空的数据
SELECT * FROM baoming where id BETWEEN 10 AND 20; //id字段10到20的数据
SELECT * FROM baoming where id not BETWEEN 10 AND 20; //除id字段10到20值的数据不显示(not代表取反)
update orders set tel=13355556666 where id=55; //修改orders表tel字段的内容
update orders set tel=tel-1 where id=55; //修改orders表tel字段,数量-1或者+s1
delete from orders where id=55; //删除一条数据
select proprice from orders_detail group by proprice having count(*)>2; ???
select concat(id,'+',tel) from orders;
set names utf8;选择数据库后设置操作编码为utf8
drop table user; 删除整个user表
select now() as '当前时间';
desc orders;当前表结构
----------事物回滚----------
start transaction; 1.开启事务
savepoint a; 2.做保存点a名称
3.操作
4.1可以回滚rollback,可以提交Commit
4.2如果觉得有问题就回滚,rollback to a;回滚a保存点
//1、链接mysql数据库
//try和if判断一样
try{
$pdo = new PDO('mysql:host=127.0.0.1;dbname=db','root','root');
}catch(PDOException $error){
echo '数据库连接失败'.$error->getMessage();
}
//2、准备mysql语句
$pre = $pdo -> prepare('select * from admin');
//3、执行mysql语句
$data=$pre->execute();
//4、如果没有拿到数据就报错
if(!$data){
echo $data->errorInfo();
}
//5、取出结果后赋值给变量
$data = $pre->fetchAll();
print_r($data);
//1、链接mysql数据库
try{
$pdo = new PDO('mysql:host=127.0.0.1;dbname=db','root','root');
}catch(PDOException $error){
echo '数据库连接失败'.$error->getMessage();
}
//2、准备mysql语句
$pre = $pdo -> prepare('insert into admin set `username`=?,`password`=?');
//3、执行mysql语句
$data=$pre->execute(['admin11',111111]);
if(!$data){
echo $pre->errorInfo();
}else{
echo $pre->rowCount();//插入了几条数据,测试用
echo $pdo->lastInsertId();//当前数据ID,测试用
}
//1、链接mysql数据库
try{
$pdo = new PDO('mysql:host=127.0.0.1;dbname=db','root','root');
}catch(PDOException $error){
echo '数据库连接失败'.$error->getMessage();
}
//2、准备mysql语句
$pre = $pdo -> prepare('insert into admin set `username`=:user,`password`=:psw');
//3、执行mysql语句
$data=$pre->execute([
':user'=>'admin22',
':psw'=>222222
]);
if(!$data){
echo $pre->errorInfo();
}else{
echo $pre->rowCount();//插入了几条数据
echo $pdo->lastInsertId();//当前数据ID
}
//1、链接mysql数据库
try{
$pdo = new PDO('mysql:host=127.0.0.1;dbname=db','root','root');
}catch(PDOException $error){
echo '数据库连接失败'.$error->getMessage();
}
//2、准备mysql语句
$pre = $pdo -> prepare('select * from baoming');
//3、执行mysql语句
$pre->execute();
$pdo->query('set names utf8');//设置字符集编码
//4、把name字段赋值给$name变量
$pre->bindColumn('name',$name);
//5、循环取值
while($pre->fetch(PDO::FETCH_ASSOC)){
echo 'name='.$name;
echo '<br>';
}
//6、关闭PDO连接,两种方式
$PDO = null;
unset($PDO);