更新
mysqli连接,推荐
$conn = mysqli_connect('127.0.0.1','root2','root2'); mysqli_select_db($conn,'jianshu'); $sql = "select * from posts"; $result = mysqli_query($conn,$sql); while($row = mysqli_fetch_assoc($result)){ var_dump($row);}
处理错误
mysqli_connect_errno()错误代码 mysqli_connect_error()错误内容 if (!$link) { exit('error('.mysqli_connect_errno().'):'.mysqli_connect_error());//不继续向下执行 //die }
设定字符集
mysqli_set_charset($conn,'utf8');
取值
mysqli_fetch_all mysqli_fetch_array mysqli_fetch_assoc mysqli_fetch_num //mysqli_fetch_array默认返回MYSQLI_BOTH //MYSQLI_ASSOC、 MYSQLI_NUM 、MYSQLI_BOTH默认 //[0] => 34 [id] => 34 [1] =>Linux常用技巧 [title] => Linux常用技巧 print_r(mysqli_fetch_all($result)); mysqli_fetch_array//默认返回两种MYSQLI_BOTH //MYSQLI_ASSOC MYSQLI_NUM MYSQLI_BOTH默认 //[0] => 34 [id] => 34 [1] =>Linux常用技巧 [title] => Linux常用技巧 print_r(mysqli_fetch_array($result,MYSQLI_NUM)); mysqli_fetch_num //获取查询结果中的一条数据,为索引数组(数据库第一条,不一定是最小或最大id) //执行后,指向下一条数据 // [0] => 34 [1] => Linux常用技巧 [2] => 28echo print_r(mysqli_fetch_row($result)); mysqli_fetch_assoc //获取查询结果中的一条数据,为关联数组(数据库第一条,不一定是最小或最大id) //执行后,指向下一条数据 // [id] => 34[title] => Linux常用技巧 [read] => 28echo print_r(mysqli_fetch_assoc($result));
关闭资源、关闭数据库
mysqli_free_result($result);//释放结果资源 mysqli_close($conn); //关闭数据库连接
mysql连接(不推荐)
$link = mysql_link('localhost','root','123') or die('error'); my_select_db('user',$link) or die('error'); $sql = ""; $result = mysql_query($sql); $count = mysql_num_rows($result); while($row = mysql_fetch_assoc($result)){ echo $row['id']; }
以前的
1、连接(mysqli方式)
$con = new mysqli("localhost", "username", "password", "databasename");
2、查询
$con->query('set names utf8;'); $sql = "SELECT * FROM tablename"; $result = $con->query($sql); $data=array(); while ($tmp=mysqli_fetch_assoc($result)) { $data[]=$tmp; } var_dump($data);
3、插入
$con->query('set names utf8;'); $sql="INSERT INTO tablename (name,telphone) VALUES ('name','telphone')"; if($result = $con->query($sql)){ echo "成功"; }else{ echo "失败"; }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
长按识别二维码并关注微信
更方便到期提醒、手机管理