Blogger Information
Blog 67
fans 0
comment 2
visits 72046
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
mysql常用语句、PDO连接mysql数据库
搁浅
Original
996 people have browsed it

mysql常用语句

  1. 注意:字符串要加'英文单引号,int数字类型可以不加,表明和字段名加`反引号(键盘1的左边),是特殊名称必须加`号,不是可以不加。
  2. insert into `baoming`(`mingzi`,`tel`) values('太阳',13011112222); //插入orders表tel字段的值
  3. INSERT INTO baoming VALUES(99,'admin99',null); //从左到右每个字段都要插入数据,不能多写字段数据,也不能少写字段数据,如果某个字段要为空就用null代替。
  4. INSERT INTO baoming set name='太阳',tele=18900008888; //这种插入更方便,适合插入字段有很多,不会很乱。
  5. select count(*) from orders; //查询表有多少条数据
  6. select max(tel) from orders; //查询表tel字段最大数值
  7. select min(tel) from orders; //查询表tel字段最小数值
  8. 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
  9. 右表,显示在一起
  10. select * from orders limit 10; //查询10条数据,或者limit 10,20(10到20条)
  11. select * from orders order by id desc; //降序查询
  12. select tel from orders where tel like `%6%`; //模糊查找数据有6的值,%相当于所有数据
  13. SELECT * from baoming where name LIKE 'o_'; //查找o开始的数据,_相当于一个数据,后面有多少写多少,写少或者写多都不能查找到数据(不建议用)
  14. select * from orders where `phone` is null; //phone字段为空的数据
  15. select * from orders where `phone` is not null; //phone字段不为空的数据
  16. SELECT * FROM baoming where id BETWEEN 10 AND 20; //id字段10到20的数据
  17. SELECT * FROM baoming where id not BETWEEN 10 AND 20; //除id字段10到20值的数据不显示(not代表取反)
  18. update orders set tel=13355556666 where id=55; //修改orders表tel字段的内容
  19. update orders set tel=tel-1 where id=55; //修改orders表tel字段,数量-1或者+s1
  20. delete from orders where id=55; //删除一条数据
  21. select proprice from orders_detail group by proprice having count(*)>2; ???
  22. select concat(id,'+',tel) from orders;
  23. set names utf8;选择数据库后设置操作编码为utf8

不常用语句

  1. drop table user; 删除整个user
  2. select now() as '当前时间';
  3. desc orders;当前表结构
  4. ----------事物回滚----------
  5. start transaction; 1.开启事务
  6. savepoint a; 2.做保存点a名称
  7. 3.操作
  8. 4.1可以回滚rollback,可以提交Commit
  9. 4.2如果觉得有问题就回滚,rollback to a;回滚a保存点
  • mysql语句优化:拿回数据的时候,需要那个字段数据就把哪一个字段写上,不然造成拿到没必要的字段数据,这样能加快读取数据速度。
  • mysql字段长度优化:在设置字段的时候,设置数据需要的长度,设置太大,读取速度会变慢,

    PDO连接mysql数据库

    1. //1、链接mysql数据库
    2. //try和if判断一样
    3. try{
    4. $pdo = new PDO('mysql:host=127.0.0.1;dbname=db','root','root');
    5. }catch(PDOException $error){
    6. echo '数据库连接失败'.$error->getMessage();
    7. }
    8. //2、准备mysql语句
    9. $pre = $pdo -> prepare('select * from admin');
    10. //3、执行mysql语句
    11. $data=$pre->execute();
    12. //4、如果没有拿到数据就报错
    13. if(!$data){
    14. echo $data->errorInfo();
    15. }
    16. //5、取出结果后赋值给变量
    17. $data = $pre->fetchAll();
    18. print_r($data);

    PDO_?号占位符

    1. //1、链接mysql数据库
    2. try{
    3. $pdo = new PDO('mysql:host=127.0.0.1;dbname=db','root','root');
    4. }catch(PDOException $error){
    5. echo '数据库连接失败'.$error->getMessage();
    6. }
    7. //2、准备mysql语句
    8. $pre = $pdo -> prepare('insert into admin set `username`=?,`password`=?');
    9. //3、执行mysql语句
    10. $data=$pre->execute(['admin11',111111]);
    11. if(!$data){
    12. echo $pre->errorInfo();
    13. }else{
    14. echo $pre->rowCount();//插入了几条数据,测试用
    15. echo $pdo->lastInsertId();//当前数据ID,测试用
    16. }

    PDO明文占位符

    1. //1、链接mysql数据库
    2. try{
    3. $pdo = new PDO('mysql:host=127.0.0.1;dbname=db','root','root');
    4. }catch(PDOException $error){
    5. echo '数据库连接失败'.$error->getMessage();
    6. }
    7. //2、准备mysql语句
    8. $pre = $pdo -> prepare('insert into admin set `username`=:user,`password`=:psw');
    9. //3、执行mysql语句
    10. $data=$pre->execute([
    11. ':user'=>'admin22',
    12. ':psw'=>222222
    13. ]);
    14. if(!$data){
    15. echo $pre->errorInfo();
    16. }else{
    17. echo $pre->rowCount();//插入了几条数据
    18. echo $pdo->lastInsertId();//当前数据ID
    19. }

    while读取数据

    1. //1、链接mysql数据库
    2. try{
    3. $pdo = new PDO('mysql:host=127.0.0.1;dbname=db','root','root');
    4. }catch(PDOException $error){
    5. echo '数据库连接失败'.$error->getMessage();
    6. }
    7. //2、准备mysql语句
    8. $pre = $pdo -> prepare('select * from baoming');
    9. //3、执行mysql语句
    10. $pre->execute();
    11. $pdo->query('set names utf8');//设置字符集编码
    12. //4、把name字段赋值给$name变量
    13. $pre->bindColumn('name',$name);
    14. //5、循环取值
    15. while($pre->fetch(PDO::FETCH_ASSOC)){
    16. echo 'name='.$name;
    17. echo '<br>';
    18. }
    19. //6、关闭PDO连接,两种方式
    20. $PDO = null;
    21. unset($PDO);
Correcting teacher:PHPzPHPz

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