美文网首页数据库技术
【LINUX】扩展mysqli与扩展pdo_mysql的方法与区

【LINUX】扩展mysqli与扩展pdo_mysql的方法与区

作者: 一生悬命Cat | 来源:发表于2019-01-30 00:24 被阅读44次

    一、php7扩展mysqli方法
    1.cd /opt/php-7.2/ext/mysqli到PHP目录的ext下的mysqli
    2./opt/php/bin/phpize执行编译后的PHP的phpize(注意是编译后的PHP目录后)
    3./configure -with-php-config=/opt/php/bin/php-config --with-mysqli=/usr/bin/mysql_config
    3.make && make install
    4.extension=mysqli添加到php.ini中
    5php -m查看是否添加到mysqli的扩展(由于PHP7取消了mysql的扩展,只能用mysqli或者pdo)

    [附带]错误解决方法
    # [在安装mysqli的时候,出现error: ext/mysqlnd/mysql_float_to_double.h: No such file or directory]
    /opt/php-7.2/ext/mysqli
    vim mysqli_api.c
    修改源码
    把第36行的
    #include “ext/mysqlnd/mysql_float_to_double.h”
    修改为
    #include “/opt/php-7.2/ext/mysqlnd/mysql_float_to_double.h”

    三PHP扩展pdo_mysql方法
    1.安装PDO组件
    cd /你的PHP源码安装地址/ext/pdo
    phpize
    ./configure --with-php-config=/编译后的PHP地址/bin/php-configmake & make install
    2.安装PDO_MYSQL组件
    cd /你的PHP源码安装地址/ext/pdo_mysql
    phpize
    ./configure --with-php-config=/编译后的PHP地址/bin/php-config --with-pdo-mysql=/usr/make & make install (由于我是用yum安装的mysql 所有会提示地址出错,后来看到别人就用--with-pdo-mysql=/usr/安装成功)果然可以。
    3.添加pdo_mysql扩展
    extension=pdo_mysql.so
    phpinfo()查看扩展

    四MySQLi和PDO的用法和区别
    连接方式
    $pdo = new PDO("mysql:host=localhost;dbname=database", 'username', 'password');
    $mysqli = mysqli_connect('localhost','username','password','database');

    PDO基本操作

    $dsn="mysql:dbname=test;host=localhost";
    $db_user='root';
    $db_pass='admin';
    try{
     $pdo=new PDO($dsn,$db_user,$db_pass);
    }catch(PDOException $e){
     echo '数据库连接失败'.$e->getMessage();
    }
    //新增
    $sql="insert into buyer (username,password,email) values ('ff','123456','admin@admin.com')";
    $res=$pdo->exec($sql);
    echo '影响行数:'.$res;
     
    //修改
    $sql="update buyer set username='ff123' where id>3";
    $res=$pdo->exec($sql);
    echo '影响行数:'.$res;
    //查询
    $sql="select * from buyer";
    $res=$pdo->query($sql);
    foreach($res as $row){
     echo $row['username'].'<br/>';
    }
    //删除
    $sql="delete from buyer where id>5";
    $res=$pdo->exec($sql);
    echo '影响行数:'.$res;
    

    mysqli基本操作

     //数据库操作类
     class DBUtil{
      private $host="localhost";
      private $username="root";
      private $password="123456";
      private $dbname="student";
      private $conn;
      public function DBUtil(){
       $this->conn=new mysqli($this->host, $this->username, $this->password,$this->dbname) or die($this->conn->connect_error);
      }
     //查询
      public function query($sql){
       $all= $this->conn->query($sql);
       return $all;
      }
     //插入,修改,删除
      public function otherOperate($sql){
       if($this->conn->query($sql)){
        if($this->conn->affected_rows>0){
          return "OK";
        }else{
          return "ERROOR";
        }
       }
      }
      public function close(){
       $this->conn->close();
      }
     }
    ?>
    

    mysql数据库基础

     
    #显示数据库
    show databases;
     
    #判断是否存在数据库wpj1105,有的话先删除
    drop database if exists wpj1105;
     
    #创建数据库
    create database wpj1105;
     
    #删除数据库
    drop database wpj1105;
     
    #使用该数据库
    use wpj1105;
     
    #显示数据库中的表
    show tables;
     
    #先判断表是否存在,存在先删除
    drop table if exists student;
     
    #创建表
    create table student(
    id int auto_increment primary key,
    name varchar(50),
    sex varchar(20),
    date varchar(50),
    content varchar(100)
    )default charset=utf8;
     
    #删除表
    drop table student;
     
    #查看表的结构
    describe student;  #可以简写为desc student;
     
    #插入数据
    insert into student values(null,'aa','男','1988-10-2','......');
    insert into student values(null,'bb','女','1889-03-6','......');
    insert into student values(null,'cc','男','1889-08-8','......');
    insert into student values(null,'dd','女','1889-12-8','......');
    insert into student values(null,'ee','女','1889-09-6','......');
    insert into student values(null,'ff','null','1889-09-6','......');
    #查询表中的数据
    select * from student;
    select id,name from student;
     
    #修改某一条数据
    update student set sex='男' where id=4;
     
    #删除数据
    delete from student where id=5;
     
    # and 且
    select * from student where date>'1988-1-2' and date<'1988-12-1';
     
    # or 或
    select * from student where date<'1988-11-2' or date>'1988-12-1';
      
    #between
    select * from student where date between '1988-1-2' and '1988-12-1';
     
    #in 查询制定集合内的数据
    select * from student where id in (1,3,5);
     
    #排序 asc 升序  desc 降序
    select * from student order by id asc;
     
    #分组查询 #聚合函数
    select max(id),name,sex from student group by sex;
     
    select min(date) from student;
     
    select avg(id) as '求平均' from student;
     
    select count(*) from student;   #统计表中总数
     
    select count(sex) from student;   #统计表中性别总数  若有一条数据中sex为空的话,就不予以统计~
     
    select sum(id) from student;
     
    #查询第i条以后到第j条的数据(不包括第i条)
    select * from student limit 2,5;  #显示3-5条数据
    

    相关文章

      网友评论

        本文标题:【LINUX】扩展mysqli与扩展pdo_mysql的方法与区

        本文链接:https://www.haomeiwen.com/subject/pjafsqtx.html