zoukankan      html  css  js  c++  java
  • 【PHP&&mysqli】

    msyqli和mysql只有一个字母的差别,真正的含义是msyql的增强版扩展。

    MySQL可以处理满足程序员对MySQL数据库操作的各种需要了,为什么还需要mysqli呢?因为mysqli支持面性对象编程,PHP的开发者为了适应当前的发展,不得不对PHP进行升级,并使其慢慢向面向对象看齐。

    虽然mysqli支持面向对象,但是mysqli扩展库考虑到PHP老程序员,同时也提供了面向过程的编程风格,这就造成了PHP中的一大奇景:同一个方法既有面向过程风格的,又有面向对象风格的。

    比如:关闭数据库连接的方法:

    面向对象函数库:
    bool mysqli::close(void)
    $mysqli=new MySQLi(xx);
    $mysqli->close();

    面向过程的函数库:
    bool mysqli_close(mysql $link)
    $conn=mysqli_connect(xxx,xx)
    mysqli_close($conn);

    同样的,我们首先应该熟练掌握对数据库的增删查改的操作。

    值得一提的是,mysqli在连接数据库的时候就必须指定连接那个数据库,而mysql则需要先连接数据库,再选择具体的数据库,使用mysql_select_db()方法。

    在案例中学习mysqli

    提醒:mysqli_connect_error()方法只能在PHP5.3.0以上的版本中使用

    一、crud操作

    interface.php

     1 <?php
     2     require_once "mysqli_tool.php";
     3     $mysqli=new mysqli_tool("localhost","root","5a6f38","users");
     4     if(mysqli_connect_error())
     5     {
     6         die("连接数据库失败!");
     7     }
     8     //先查询一次
     9     echo "查询操作:";
    10     printf_all($mysqli);
    11     
    12 
    13     //进行插入操作
    14     $sql="insert into user1(name,password,email,age) values ('艳荣',md5('sssx'),'1649653731@qq.com',23)";
    15     echo "数据插入操作:";
    16     $mysqli->update($sql);
    17     printf_all($mysqli);
    18     
    19     //进行修改操作
    20     $num=$mysqli->getinsertid();
    21     $sql="update user1 set email='2632979128@sohu.com'where id='".$mysqli->getinsertid()."'";
    22     $mysqli->update($sql);
    23     echo "对刚插入的数据进行修改操作:";
    24     printf_all($mysqli);
    25     
    26     //进行删除操作
    27     echo "对刚插入的数据进行删除操作:";
    28     $sql="delete from user1 where id='".$num."'";
    29     $res=$mysqli->update($sql);
    30     printf_all($mysqli);
    31 
    32     function printf_all($mysqli)
    33     {
    34         echo ",查询后结果是:<br/>";
    35         $sql="select * from user1 order by id";
    36         $mysqli->search($sql);
    37         while($row=$mysqli->getres()->fetch_row())
    38         {
    39             foreach($row as $key=>$value)
    40             {
    41                 echo "$value--";
    42             }
    43             echo "<br/>";
    44         }
    45         echo "<br/><br/><br/>";
    46         $mysqli->close_res($mysqli->getres());
    47     }
    48 
    49     $mysqli->close_conn($mysqli->getconn());
    50 ?>
    View Code

    mysqli_tool.php

     1 <?php
     2     class mysqli_tool
     3     {
     4         private $host,$name,$pw,$db_name,$mysqli;
     5         private $conn,$res;
     6         public function __construct($host,$name,$pw,$db_name)
     7         {
     8             $this->host=$host;
     9             $this->name=$name;
    10             $this->pw=$pw;
    11             $this->db_name=$db_name;
    12             $this->mysqli=new mysqli($this->host,$this->name,$this->pw,$this->db_name);
    13             $this->mysqli->query("set names utf8");
    14         }
    15         //查询功能
    16         public function search($sql)
    17         {
    18             $this->res=$this->mysqli->query($sql,$this->conn);
    19             if(!$this->res)
    20             {
    21                 die("数据查询失败!");
    22             }
    23             else
    24             {
    25                 if($this->mysqli->affected_rows==0)
    26                 {
    27                     echo "没有符合条件的数据!";
    28                 }
    29                 else
    30                 {
    31                     return $this->res;
    32                 }
    33             }
    34         }
    35 
    36         public function update($sql)
    37         {
    38             $this->res=$this->mysqli->query($sql,$this->mysqli->conn);
    39             //echo "受影响的行数是:".$this->mysqli->affected_rows."<br/>";
    40             if(!$this->res)
    41             {
    42                 echo "数据更新失败!";
    43             }
    44         }
    45 
    46         public function getinsertid()
    47         {
    48             return $this->mysqli->insert_id;
    49         }
    50         public function getconn()
    51         {
    52             return $this->conn;
    53         }
    54         public function getres()
    55         {
    56             return $this->res;
    57         }
    58         public function close_conn($conn)
    59         {
    60             $this->mysqli->close();
    61         }
    62         public function close_res($res)
    63         {
    64             $this->res->free();
    65         }
    66     }
    67     
    68 ?>
    View Code

     二、事务回滚操作

    如果不能回滚,非常有可能是存储引擎的问题,详情:mysql myisam和innodb的区别

     1 <?php
     2 
     3 
     4 
     5 //有疑问:不能实现回滚的原因是什么?数据库的存储引擎不符合要求,不支持事务处理
     6     //创建数据库连接对象
     7     $mysqli=new mysqli('localhost','root','5a6f38','users');
     8     //将mysql数据库的自动提交设置false
     9     $mysqli->autocommit(false);
    10     //设置sql指令
    11     $sql1="update account set account=account-2 where name='zhangsan'";
    12     $sql2="update account set account=account+2 where name='lisi'";
    13     //执行指令
    14     $r1=$mysqli->query($sql1);
    15     $r2=$mysqli->query($sql2);
    16     //判断结果
    17     if(!$r1||!$r2)
    18     {
    19         //两个sql语句执行中至少有一个错误,回滚处理
    20         $mysqli->rollback();
    21         echo "转账失败!回滚处理!";
    22     }
    23     else
    24     {
    25         $mysqli->commit();
    26         $mysqli->close();
    27         echo "转账成功!";
    28     }
    29 ?>
    View Code

    三、批量查询

    mysqli相对于mysql扩展库,有一个比较大的改进:支持批量查询

    实例代码如下:

     1 <?php
     2     //1.创建msyqli对象
     3     $mysqli=new mysqli("localhost","root","5a6f38","users");
     4     if(mysqli_connect_error())
     5     {
     6         die("数据库连接失败:".mysqli_connect_error());
     7     }
     8     //2.创建批量查询语句
     9     $sqls="select * from user1;";
    10     $sqls.="show columns from user1";
    11     //3.处理结果
    12     $res=$mysqli->multi_query($sqls);
    13     if($res)//如果条件成立说明至少有一个结果被返回
    14     {
    15         do
    16         {    
    17             echo "*********************查询到的新表******************<br/>";
    18             echo "<br/>标志变量<br/>";
    19             $result=$mysqli->store_result();//$result是mysqli result对象
    20             while($row=$result->fetch_row())
    21             {
    22                 foreach($row as $value)
    23                 {
    24                     echo "--$value";
    25                 }
    26                 echo "<br/>";
    27             }
    28             $result->free();
    29             if(!$mysqli->more_results())//如果没有下一个查询,则停止取下一个查询的结果。
    30             {
    31                 break;
    32             }
    33             echo "<br/><br/>";
    34 
    35         }
    36         while($mysqli->next_result());//指针后移,并判断是否为空
    37     }
    38     else
    39     {
    40         echo "数据查询失败!";
    41     }
    42     $mysqli->close();
    43 ?>
    View Code

    四、预编译技术

    大概有很多人没听说过这个东西,其实我也是不太明白,毕竟sql是一种解释型的语言,不需要编译就可以被dbms执行,但是在这里我们权且将它当做可以被编译,这样理解起来的话就比较容易了。

    问题的提出:

    现在要向数据库中添加100条数据,我们该怎么实现?

    方法1:使用for循环,依次添加100次

    方法2:使用批量查询技术,将100个sql语句拼接起来之后最后打包执行。

    方法3:使用预编译技术。

    这里我们使用预编译技术,使用预编译技术有什么好处?

    1.防止sql注入

    2.执行速度快

    有和两条好处就足够了。

    预编译流程:将sql语句发送到dbms,由dbms编译好并保存起来。我们只需要将数据发送到dbms即可,而不需要将整条sql语句发送到dbms,这样就大大提高了执行效率,这对于大量数据的插入尤其明显。

    下面将展示各种查询操作,但要先强调:使用完之后要多关闭一个资源:dbms存储的预编译文件。

    基本流程:预编译文件-》绑定参数-》执行-》绑定参数-》执行。。。。

    使用预编译技术实现批量插入:

     1 <?php
     2     //创建mysqli对象
     3     $mysqli=new mysqli("localhost","root","5a6f38","users");
     4     //创建预编译对象
     5     $sql="insert into user1 (name,password,age,email) values (?,?,?,?)";
     6     $mysqli_stmt=$mysqli->prepare($sql);
     7 
     8 
     9     //创建参数并绑定
    10     $name="lisi";
    11     $password="6f6f6f";
    12     $age=40;
    13     $email="1659653731@qq.com";
    14     $mysqli_stmt->bind_param("ssis",$name,$password,$age,$email);
    15 
    16     //执行函数
    17     execute_prepared($mysqli_stmt,$mysqli);
    18 
    19     //创建参数并绑定
    20     $name="zhangsan";
    21     $password="6f6f6f";
    22     $age=40;
    23     $email="1659653731@qq.com";
    24     $mysqli_stmt->bind_param("ssis",$name,$password,$age,$email);
    25 
    26     //执行函数
    27     execute_prepared($mysqli_stmt,$mysqli);
    28 
    29     //反复执行即可完成插入操作
    30 
    31     $mysqli->close();
    32 
    33 
    34 
    35     function execute_prepared(&$mysqli_stmt,&$mysqli)
    36     {
    37         //执行语句
    38         $result=$mysqli_stmt->execute();
    39         //验证是否成功
    40         if(!$result)
    41         {
    42             die("操作失败!".$mysqli_stmt->error);
    43         }
    44         else
    45         {
    46             echo "操作成功!<br/>";
    47             //打印验证
    48             printf_all($mysqli);
    49         }
    50     }
    51     function printf_all(&$mysqli)
    52     {
    53         $sql="select * from user1";
    54         $res=$mysqli->query($sql);
    55         while($row=$res->fetch_row())
    56         {
    57             foreach($row as $value)
    58             {
    59                 echo "$value&nbsp;&nbsp;&nbsp;";
    60             }
    61             echo "<br/>";
    62         }
    63         $res->free();
    64     }
    65 ?>
    View Code

    使用预编译技术实现批量查询

     1 <?php
     2     //创建mysali对象
     3     $mysqli=new mysqli("localhost","root","5a6f38","users");
     4     if(mysqli_connect_error())
     5     {
     6         die("数据库连接失败!".mysqli_connect_error());
     7     }
     8     //创建预编译对象
     9     $sql="select * from user1 where id>?";
    10     $mysqli_stmt=$mysqli->prepare($sql);
    11     //绑定参数
    12     $id=15;
    13     $mysqli_stmt->bind_param("i",$id);
    14     //绑定结果集
    15     $mysqli_stmt->bind_result($id,$name,$password,$email,$age);
    16     //执行
    17     $mysqli_stmt->execute();
    18     //取出绑定的值
    19     while($mysqli_stmt->fetch())
    20     {
    21         echo "$id&nbsp;&nbsp;&nbsp;$name&nbsp;&nbsp;&nbsp;
    22         $password&nbsp;&nbsp;&nbsp;$email&nbsp;&nbsp;&nbsp;$age<br/>";
    23     }
    24     //释放结果集
    25     $mysqli_stmt->free_result();
    26     //删除在数据库中的预编译文件
    27     $mysqli_stmt->close();
    28     //关闭和数据库的连接
    29     $mysqli->close();
    30 ?>
    View Code

    五、获取表头信息和行数、列数并打印表格

     1 <?php
     2     $database="users";
     3     $sql="select * from user1";
     4     printf_table($database,$sql);
     5     function printf_table($database,$sql)
     6     {
     7         //创建$mysqli对象。
     8         $mysqli=new mysqli("localhost","root","5a6f38",$database);
     9         if(mysqli_connect_error())
    10         {
    11             die("数据库连接失败!失败信息:".mysqli_connect_error());
    12         }
    13         //获取结果集
    14         $res=$mysqli->query($sql);
    15         //获取行数
    16         $row_nums=$res->num_rows;
    17         //获取列数
    18         $column_nums=$res->field_count;
    19         //打印表头信息
    20         echo "<center><table cellpadding='10' cellspacing='1' bgcolor='black'>";
    21         echo "<tr bgcolor='white'>";
    22         while($field=$res->fetch_field())//$field是列属性对象
    23         {
    24             echo "<th>$field->name</th>";
    25         }
    26         echo "</tr>";
    27         //取出表体数据
    28         while($row=$res->fetch_row())
    29         {
    30             echo "<tr bgcolor='white'>";
    31             foreach($row as $value)
    32             {
    33                 echo "<td>$value</td>";
    34             }
    35             echo "</tr>";
    36         }
    37         echo "</table></center>";
    38         //释放资源
    39         $res->free();
    40         //断开连接
    41         $mysqli->close();
    42     }
    43 ?>
    View Code
  • 相关阅读:
    spring中Bean的懒加载
    ActiveMQ学习教程
    Maven中解决jar包冲突的三种方式
    Spring的日志管理
    mybatis使用collection查询集合属性规则
    springcloud之Feign伪装
    springcloud之Hystrix熔断入门
    springcloud的负载均衡Ribbon入门
    springcloud的Eureka启动时报错:Connection refused: connect
    springboot启动过程中出现You must configure either the server or JDBC driver (via the serverTimezone configuration
  • 原文地址:https://www.cnblogs.com/kuangdaoyizhimei/p/3969806.html
Copyright © 2011-2022 走看看