首页 > 代码库 > mysqli练手

mysqli练手

技术分享

1
<?php 2 header("Content-Type:text/html;charset=utf-8"); 3 //mysqli操作数据库 4 //1.创建对象 5 $mysqli = new mysqli ("localhost","root","","test"); 6 //验证连接是否成功 7 if (!$mysqli){ 8 # code... 9 die("连接失败".$mysqli->connect_error);10 }11 //2.操作数据库12 $sql = "select * from user";13 $res = $mysqli->query($sql);14 // var_dump($res);15 //3.处理结果16 if ($res) {17 # code...18 //fetch_row()记得加上()19 while ($row = $res->fetch_row()) {20 # code...21 foreach ($row as $key => $value) {22 # code...23 echo "$value--";24 }25 echo "<br />";26 }27 }else{28 echo "失败";29 }30 //4.关闭资源31 $res->free();32 $mysqli->close();33 ?>

 

mysqli练手