服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - PHP教程 - 实例讲解php数据访问

实例讲解php数据访问

2021-01-16 15:33陌上初薰 PHP教程

这篇文章主要以实例讲解的方式为大家详细介绍了php数据访问,数据访问有两种方式,本文为大家揭晓,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了两种php数据访问方式,大家可以进行比较,分析两种方式的异同,最后为大家提供了一个小练习,具体内容如下

方式一:已过时,只做了解

1.造一个连接(建立通道)

$db=mysql_connect("localhost","root","123");     //括号内是“服务器地址”,“用户名”,“密码”

2.选择操作哪个数据库

mysql_select_db("mydb","$db");

3.写sql语句

$sql="select * from info";

4.执行sql语句

$result=mysql_query($sql);      //query 有查询之意

5.从结果集($result)中取数据

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$row=mysql_fetch_row($result);  //每执行一次读取一行数据
 
$row1=mysql_fentch_row($result);  //执行第二条数据
 
var_dump($row);
 
//读取全部数据用循环:
 
while($row=mysql_fetch_row($result))
 
{
 
  var_dump($row);  
 
}

方法二:面向对象

1.造一个连接对象:

$db=new mysqli("localhost","root","123","mydb")  //括号内的内容依次为“服务器地址”,“用户名”,“密码”,“数据库名称”

2.判断连接是否出错:

2.1 mysqli_connect_error();  //代表连接出错

2.2

if(mysqli_connect_erroe())

       {

    echo "连接失败!";

    exit();  //退出程序

        }

  2.3 !mysqli_connect_error or die ("连接失败!"); //“or”前面代表连接正确,后面代表连接失败

3. 写sql语句:

$sql="select * from nation";

4. 执行sql语句:如果执行成功返回结果集对象,如果执行失败返回false

$result=$db->query($sql);

5.从结果集中读取数据,先判断是否有数据

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
if($result)
 
{
 
  //返回一行数据的索引数组,每次执行返回一条数据
 
   var_dump($result->fetch_row());
 
  while($row=$result->fetch_row)
 
  {
 
    var_dump($row);
 
  }
 
  //返回一行数据的关联数组,每次执行返回一条数据
 
  var_dump($result->fetch_row());
 
  //通过二维数组返回所有数据
 
  var_dump($result->fetch_all());
 
  //以对象的方式返回一行数据
 
  var_dump($result->fetch_object());
 
}

练习:

1.以下拉菜单的形式在页面显示nation表

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$db=new mysqli("localhost","root","","mydb");
 
!mysqli_connection_erroe() or die ("连接失败!");
 
$sql="select*from nation";
 
$result=$db->query($sql);
 
if($result)
 
{
 
  $att=$result->fetch_all();
 
  echo "<select>";
 
  foreach ($att as $value)
 
  {
 
    echo "<option value='{$value[0]}'>{$value[1]}</option>";
 
  }
 
  echo "</select>";
 
}

实例讲解php数据访问

2. 把info表查出来,以表格的形式显示

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
$db=new mysqli("localhost","root","","mydb");
 
!mysqli_connecton_error() or die("连接失败!");
 
$sql="select * from info";
 
$result=$bd->query($sql);
 
if($result)
 
{
 
$att=$result->fetch_all();
 
echo "<table border='1' width='100%' cellpadding='0' cellspacing='0'>";
 
echo "<tr><td>代号</td><td>姓名</td><td>性别</td><td>民族</td><td>生日</td></tr>";
 
foreach ($att as $value)
 
{
 
 echo "<tr>
 
<td>{$value[0]}</td>
 
<td>{$value[1]}</td>
 
<td>{$value[2]}</td>
 
<td>{$value[3]}</td>
 
<td>{$value[4]}</td>
 
</tr>";
 
}
 
echo "</table>";
 
}
 
 
 
//也可以用for循环
 
if($result)
{
  $arr=$result->fetch_all();
  echo "<table border='1' width='100%' cellpadding='0' cellspacing='0'>";
  echo "<tr><td>code</td><td>name</td><td>sex</td><td>nation</td><td>birthday</td></tr>";
  for($i=0;$i<count($arr);$i++)
  {
    echo "<tr>
    <td>{$arr[$i][0]}</td>
    <td>{$arr[$i][1]}</td>
    <td>{$arr[$i][2]}</td>
    <td>{$arr[$i][3]}</td>
    <td>{$arr[$i][4]}</td>
    </tr>";
  }
  echo "</table>";
}

实例讲解php数据访问

以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。

延伸 · 阅读

精彩推荐