• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 3年前 (2022-01-26) 31次浏览 已收录 0个评论

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

<?php<BR>    header("Content-type: text/html;charset=utf-8");<BR>    //mysqli操作mysql数据库(面向对象方式)<BR>    //1、创建MySQLi对象<BR>    $mysqli =new MySQLi("localhost","root","root","test");<BR>    if($mysqli->connect_error){<BR>        die("连接失败".$mysqli->connect_error);<BR>    }<BR>    //2、操作数据库(发送sql)<BR>    $sql="select *from user1";<BR>    //3、处理结果<BR>    $res =$mysqli->query($sql);<BR>    //var_dump($res);<BR>    //fetch_assoc \fetch_array \fetch_object<BR>    while($row=$res->fetch_row()){<BR>        var_dump($row);<BR>/*        foreach($row as $val){<BR>            echo '--'.$val;<BR>        }<BR>        echo '<br />';*/<BR>    }<BR>    //4、关闭资源<BR>    $res->free();<BR>    $mysqli->close();<BR>?><BR>


下面是面向过程的

<?php<BR>    header("Content-type: text/html;charset=utf-8");<br><br>    $mysqli=mysqli_connect("localhost","root","root","test");<BR>    if(!$mysqli){<BR>        die("连接失败".mysqli_connect_error());<BR>    }<BR>    $sql="select *from user1";<BR>    $res=mysqli_query($mysqli,$sql);<BR>    //var_dump($res);<BR>    while($row=mysqli_fetch_row($res)){<br><br>        foreach ($row as $val){<br><br>            echo '-'.$val;<BR>        }<BR>        echo '<br />';<BR>    }<BR>    //释放内存<BR>    mysqli_free_result($res);<BR>    mysqli_close($mysqli);<BR>?><BR>


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

<?php<br><br>    //使用mysqli 扩展库对mysql的crud 操作<BR>    header("Content-type: text/html;charset=utf-8");<BR>    $mysqli = new MySQLi("localhost","root","root","test");<BR>    if($mysqli->connect_error){<BR>        die("连接失败".$mysql->connect_error);<BR>    }<BR>    //增加一条记录<BR>    //$sql="insert into user1 (name,password,email,age) values ('lucy',md5('lucy'),'[email protected]',17)";<BR>    //删除一条记录<BR>    //$sql="delete from user1 where id =80";<BR>    //更新一条记录<BR>    $sql="update user1 set age=20 where id=7";<BR>    $res=$mysqli->query($sql);<BR>    if(!$res){<BR>        echo "操作失败".$mysqli->error;<BR>    }else{<BR>        if($mysqli->affected_rows>0){<BR>            echo "成功";<BR>        }else{<BR>            echo "没有行受影响";    <BR>        }<BR>    }<BR>    //关闭资源<BR>    $mysqli->close();<BR>?><BR>


3、进¥本文来源gaodai#ma#com搞@@代~&码网^搞gaodaima代码行封装

<?<BR>    class SqlHelper{<br><br>        private $mysqli;<BR>        //这里先写死,以后写死的东西用一个文件来配置<BR>        private static $host="localhost";<BR>        private static $user="root";<BR>        private static $pwd="root";<BR>        private static $db="test";<BR>        public function __construct(){<br><br>            $this->mysqli=new MySQLi(self::$host,self::$user,self::$pwd,self::$db);<BR>            if($this->mysqli->connect_error){<BR>                die("连接失败".$this->mysqli->connect_error);<BR>            }<BR>            //设置字符集<BR>            $this->mysqli->query("set names utf8");<BR>        }<BR>        //dql operate<BR>        function execute_dql($sql){<BR>            $res =$this->mysqli->query($sql) or die($this->mysqli->error);<BR>            return $res;        <BR>        }<BR>        //dml operate<BR>        function execute_dml($sql){<BR>            $res =$this->mysqli->query($sql) or die($this->mysqli->error);<br><br>            if(!$res){<BR>                return 0;//失败<BR>            }else{<BR>                if($this->mysqli->affected_rows>0){<BR>                    return 1;//成功<BR>                }else{<BR>                    return 2;//没有行到影响<BR>                }<BR>            }<BR>        }<BR>    }<BR>?><BR>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:解析在PHP中使用mysqli扩展库对mysql的操作_php技巧
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址