PHP mysqli扩展库与mysql用法对比

1、在PHP中 使用mysqli扩展库对mysql 的dql操作

 代码如下 复制代码

<?php

    header("Content-type: text/html;charset=utf-8");
    //mysqli操作mysql数据库(面向对象方式)

    //1、创建MySQLi对象

    $mysqli =new MySQLi("localhost","root","root","test");
    if($mysqli->connect_error){
        die("连接失败".$mysqli->connect_error);
    }

    //2、操作数据库(发送sql)
    $sql="select *from user1";

    //3、处理结果
    $res =$mysqli->query($sql);
    //var_dump($res);
    //fetch_assoc fetch_array fetch_object
    while($row=$res->fetch_row()){

        var_dump($row);

/*        foreach($row as $val){
            echo '--'.$val;
        }
        echo '<br/>';*/
    }
    //4、关闭资源
    $res->free();
    $mysqli->close();
?>

下面是面向过程的

 

 代码如下 复制代码

<?php

    header("Content-type: text/html;charset=utf-8");
   
    $mysqli=mysqli_connect("localhost","root","root","test");
    if(!$mysqli){
        die("连接失败".mysqli_connect_error());
    }

    $sql="select *from user1";

    $res=mysqli_query($mysqli,$sql);
    //var_dump($res);

    while($row=mysqli_fetch_row($res)){
       
        foreach ($row as $val){
           
            echo '-'.$val;
        }
        echo '<br/>';
    }

    //释放内存
    mysqli_free_result($res);
    mysqli_close($mysqli);
?>

2、在PHP中 使用mysqli扩展库对mysql 的dml操作

 代码如下 复制代码

<?php
   
    //使用mysqli 扩展库对mysql的crud 操作
    header("Content-type: text/html;charset=utf-8");
    $mysqli = new MySQLi("localhost","root","root","test");

    if($mysqli->connect_error){
        die("连接失败".$mysql->connect_error);
    }

    //增加一条记录
    //$sql="insert into user1 (name,password,email,age) values ('lucy',md5('lucy'),'lucy@163.com',17)";
    //删除一条记录
    //$sql="delete from user1 where id =80";
    //更新一条记录
    $sql="update user1 set age=20 where id=7";

    $res=$mysqli->query($sql);
    if(!$res){
        echo "操作失败".$mysqli->error;
    }else{
        if($mysqli->affected_rows>0){
            echo "成功";
        }else{
            echo "没有行受影响";   
        }
    }

    //关闭资源
    $mysqli->close();
?>

3、进行封装

 

 代码如下 复制代码

<?

    class SqlHelper{
       
        private $mysqli;
        //这里先写死,以后写死的东西用一个文件来配置
        private static $host="localhost";
        private static $user="root";
        private static $pwd="root";
        private static $db="test";

        public function __construct(){
           
            $this->mysqli=new MySQLi(self::$host,self::$user,self::$pwd,self::$db);
            if($this->mysqli->connect_error){
                die("连接失败".$this->mysqli->connect_error);
            }
            //设置字符集
            $this->mysqli->query("set names utf8");
        }

        //dql operate
        function execute_dql($sql){

            $res =$this->mysqli->query($sql) or die($this->mysqli->error);
            return $res;       
        }

        //dml operate
        function execute_dml($sql){

            $res =$this->mysqli->query($sql) or die($this->mysqli->error);
           
            if(!$res){
                return 0;//失败
            }else{
                if($this->mysqli->affected_rows>0){
                    return 1;//成功
                }else{
                    return 2;//没有行到影响
                }
            }
        }
    }
?>

时间: 2024-09-08 08:37:21

PHP mysqli扩展库与mysql用法对比的相关文章

解析在PHP中使用mysqli扩展库对mysql的操作

本篇文章是对在PHP中使用mysqli扩展库对mysql的操作进行了详细的分析介绍,需要的朋友参考下   1.在PHP中 使用mysqli扩展库对mysql 的dql操作 复制代码 代码如下: <?php     header("Content-type: text/html;charset=utf-8");     //mysqli操作mysql数据库(面向对象方式)     //1.创建MySQLi对象     $mysqli =new MySQLi("localh

解析在PHP中使用mysqli扩展库对mysql的操作_php技巧

1.在PHP中 使用mysqli扩展库对mysql 的dql操作 复制代码 代码如下: <?php    header("Content-type: text/html;charset=utf-8");    //mysqli操作mysql数据库(面向对象方式)    //1.创建MySQLi对象    $mysqli =new MySQLi("localhost","root","root","test&qu

PHP mysqli扩展库 预处理技术的使用分析_Mysql

1.使用mysqli扩展库 预处理技术 mysqli stmt 向数据库添加3个用户 复制代码 代码如下: <?php     //mysqli扩展库 预处理技术 mysqli stmt 向数据库添加3个用户    //1.创建mysqli对象    $mysqli = new MySQLi("localhost","root","root","test");    if($mysqli->connect_erro

php结合mysql与mysqli扩展处理事务的方法_php技巧

本文实例讲述了php结合mysql与mysqli扩展处理事务的方法.分享给大家供大家参考,具体如下: 以下只是展示如何应用,具体用的时候要加上判断,如果都执行成功则提交,否则回滚 看前先分清mysqli与mysql扩展是不一样的 mysqli扩展处理事物: $mysqli=new mysqli('localhost','root','123456','test'); $mysqli->autocommit(false);//开始事物 $query="update a set money=m

linux中通过phpize添加PHP扩展openssl、mysql

phpize phpize 命令是用来准备 PHP 扩展库的编译环境的.下面例子中,扩展库的源程序位于 extname 目录中: $ cd extname $ phpize $ ./configure $ make # make install 成功的安装将创建 extname.so 并放置于 PHP 的扩展库目录中.需要调整 php.ini,加入 extension=extname.so 这一行之后才能使用此扩展库. 如果系统中没有 phpize 命令并且使用了预编译的包(例如 RPM),那要

php添加mysqli扩展

任何php的扩展都可以在php的源码包内单独编译安装 此处因phpmyadmin需mysqli扩展,编译该模块安装,过程及一些问题记录如下: php编译安装路径/usr/local/php5.6 mysql编译安装路径/usr/local/mysql5.6 进入php源码安装包的mysqli扩展路径下 # cd /tmp/php-5.6.15/ext/mysqli 生成configure文件 # /usr/local/php5.6/bin/phpize 配置 # ./configure --wi

PHP下使用mysqli的函数连接mysql出现warning: mysqli::real_connect(): (hy000/1040): ..._php实例

背景:把mysql换成mysqli时出现,连接数过多,其实际上并不是,原因是我挪动了一下php的sock文件位置导致,因这几个socket修改没有修改完全,于是出现了too many connections ,从mysql里show processlist并没有发现真的有连接,其实用tshark抓下包估计能看到(http://justwinit.cn/post/7458/),并没有发出请求,而估计是mysqli的客户端自己报出来的,别看这个问题小,搞了老半天,都想重新安装Php了,发现原来是路径

WDCP面板系统安装mysqli扩展的方法

下载mysqli扩展且安装 mysqli的安装: wget -c http://hubeidc.com/dl/wdcp/mysqli/mysqli_ins.sh && chmod 755 mysqli_ins.sh && ./mysqli_ins.sh 这样的话就不会出现安装报错了,官方提供的脚本有的主机会报错的. 一般安装错误的解决办法: A: - 如果安装时候提示mysql config not found 解决方法: 在mysqli_ins.sh脚本中./config

linux下php添加mysqli拓展库时,编译报错

问题描述 linux下php添加mysqli拓展库时,编译报错 php和mysql都是已经编译安装好了,现在想要给php添加mysqli 拓展库,结果在make的时候报错 cd /usr/local/src/php-5.6.19/ext/mysqli /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config --with-mysqli=/usr/local/mysql/bin/mys