1、在PHP中 使用mysqli扩展库对mysql 的dql操作
代码如下 | 复制代码 |
<?php header("Content-type: text/html;charset=utf-8"); //1、创建MySQLi对象 $mysqli =new MySQLi("localhost","root","root","test"); //2、操作数据库(发送sql) //3、处理结果 var_dump($row); /* foreach($row as $val){ |
下面是面向过程的
代码如下 | 复制代码 |
<?php header("Content-type: text/html;charset=utf-8"); $sql="select *from user1"; $res=mysqli_query($mysqli,$sql); while($row=mysqli_fetch_row($res)){ //释放内存 |
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){ //增加一条记录 $res=$mysqli->query($sql); //关闭资源 |
3、进行封装
代码如下 | 复制代码 |
<? class SqlHelper{ public function __construct(){ //dql operate $res =$this->mysqli->query($sql) or die($this->mysqli->error); //dml operate $res =$this->mysqli->query($sql) or die($this->mysqli->error); |