![]()
1 1. 编写php程序完成对用户表的显示
2 代码
3 <?php
4 //mysql扩展库操作mysql数据库步骤如下
5 //1. 获取连接
6
7 $conn=mysql_connect("127.0.0.1","root","root");
8 if(!$conn){
9 die("连接失败".mysql_error());
10 }
11
12 //2. 选择数据库
13 mysql_select_db("test");
14 //3. 设置操作编码(建议有)!!!
15 mysql_query(“set names utf8”); //保证我们的php程序是按照utf8码操作.
16 //4. 发送指令sql (ddl 数据定义语句 , dml(数据操作语言 update insert ,delete) ,dql (select ), dtl 数据事务语句 rollback commit... )
17 $sql="select * from user1";
18 //函数
19 //$res 表示结果集,你可以简单的理解就是 一张表.
20 $res=mysql_query($sql,$conn);
21 //var_dump($res); //mysql result 资源类型
22
23 //5. 接收返回的结果,并处理.(显示)
24 // mysql_fecth_row 会依次取出$res结果集的下一行数据,赋值给$row
25 // $row就是一个数组, 样式array(5) { [0]=> string(1) "1" [1]=> string(2) "zs" [2]=> string(32) "e10adc3949ba59abbe56e057f20f883e" [3]=> string(11) "zs@sohu.com" [4]=> string(2) "30" }
26 //mysql_fetch_assoc mysql_fetch_array
27 while($row=mysql_fetch_row($res)){
28 //第一种取法是 同 $row[$i]
29 //echo "<br/> $row[0]--$row[1]--$row[2]";
30 //echo "<br/>";
31 //var_dump($row);
32 //第二种取法
33 foreach($row as $key => $val){
34 echo "--$val";
35 }
36 echo "<br/>";
mysql_free_result($res); //释放内存
//mysql_close($conn);可写可不写
37 }