mysqli扩展库操作

来源:互联网 发布:惠州政府网络问政平台 编辑:程序博客网 时间:2024/06/04 20:09
<?php
       header("Content-type: text/html;charset=utf-8");    //设置编码方式
   
      
       //1.创建MySQLi 对象
       $mysqli=new MySQLi("localhost","root","root","test");
       //验证是否ok
       if($mysqli->connect_error){
              die("连接失败".$mysqli->connect_error);
       }
      
     
       $sql="select * from user1";       //操作数据库
    
       $res=$mysqli->query($sql);     //$res 是结果集.mysqli result
       //用var_dump($res);可以检验res的具体情况
       //3. 处理结果 mysql_fetch_row();
       while($row=$res->fetch_row()){
              
              foreach($row as $key=>$val){
                     echo "--$val";
              }
              echo "<br/>";
       }
       //4. 关闭资源
       //释放内存
       $res->free();
       //关闭连接
       $mysqli->close();
?>


使用面向过程的方式
代码:
//1.得到mysqli连接
       header("Content-type: text/html;charset=utf-8");
       $mysqli=mysqli_connect("localhost","root","root","test");
       if(!$mysqli){
              die("连接失败".mysqli_connnect_error($mysqli));
       }
       //2.向数据库发送sql语句(ddl,dml dql ...)
       $sql="select * from user1";
       $res=mysqli_query($mysqli,$sql);
       //var_dump($res);
       //3.处理得到的结果
       //循环取出$res中的数据mysqli_fetch_row mysql_fetch_row
       while($row=mysqli_fetch_row($res)){
              
              foreach($row as $key=>$val){
                     echo "--$val";
              }
              echo "<br/>";
       }
       //4.关闭资源
       mysqli_free_result($res);
       mysqli_close($mysqli);


0 0
原创粉丝点击