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>