标签:ble 数据库服务器 相关 语句 height var size while 提交
使用mysql_fetch_object()函数 同样可以获取差选结果集中的数据,跟上一篇文章中介绍的函数是类似的,下面我们通过同一个实例的不同方法了解这两个函数在使用上的区别。
首先我们看下该函数的语法格式如下:大理石平台怎么样
|
1
|
object mysql_fetch_object(resource result)
|
注意:
这个扩展是在PHP 5.5.0过时,它是在PHP 7.0.0删除。相反,mysqli扩展或pdo_mysql应使用。参见MySQL:选择API指南和相关FAQ以获取更多信息。
mysql_fetch_object()函数和mysql_fetch_array()函数类似,只是有一点区别,前者返回的是一个对象而不是数组,该函数只通过字段名来访问数组,使用下面的格式获取结果集中行的元素值。
例如,如果从某数据表中检索 id 和 name值,可以用$row->id 和 $row->name 访问行中的元素值。
注意:
本函数返回的字段也是区分大小写,这是初学者学习编程最容易忽视的问题。
下面的实例通过mysql_fetch_object()函数获取结果集中的数据信息,然后使用 echo语句从结果集中以“结果集->列名”的形式输出个字段所对应的图书信息。
具体步骤如下:
1.创建一个PHP动态页面,命名index.php,在index.php中添加一个表单,一个文本框以及一个提交按钮,具体代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<html>
<body>
<!--上传文件表单-->
<form method="post" action="" name = form1>
<table>
<tr>
<td width="605" height="51" bgcolor="#CC99FF">
<p align="center">请输入查询内容
<input type="text" name="txt_book" id="txt_book" size="25">
<input type="submit" name="Submit" value="查询">
</p>
</td>
</tr>
</table>
</form>
</body>
</html>
|
2.连接到MySQL数据库服务器,选择数据库 php_cn,设置数据库的编码格式为GB2312。具体代码如下:
|
1
2
3
4
5
6
|
<?php
header("Content-Type:text/html; charset=utf-8");
$link = mysql_connect("localhost","root","root")or die("连接数据库失败".mysql_error());
mysql_select_db("php_cn",$link);
mysql_query("set names gb2312");
?>
|
3.使用mysql_fetch_object()函数获取查询结果集中的数据,其返回的值为一个对象:
|
1
2
3
4
5
6
7
8
9
10
|
<?php
header("Content-Type:text/html; charset=utf-8");
$sql = mysql_query("select from tb_book");
$info = mysql_fetch_object($sql);
if($_POST[‘Submit‘]=="查询"){
$txt_book = $_POST[‘txt_book‘];
$sql = mysql_query("select * from tb_book where bookname like ‘%".trim($txt_book)."%‘");
$info = mysql_fetch_array($sql);
}
?>
|
4.使用 do...while循环语句,“结果列->列名”的方式输出结果集中的图文信息,代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?php
do {
?>
<table>
<tr align="left" bgcolor="#FFFFFF">
<td height="20" align="center"><?php echo $info->id; ?></td>
<td height="20" align="center"><?php echo $info->bookname; ?></td>
<td height="20" align="center"><?php echo $info->data; ?></td>
<td height="20" align="center"><?php echo $info->price; ?></td>
<td height="20" align="center"><?php echo $info->maker; ?></td>
<td height="20" align="center"><?php echo $info->publisher; ?></td>
</tr>
</table>
<?php
}while($info = mysql_fetch_object($sql));
?>
|
使用mysql_fetch_object()函数获取结果集中一行作为对象
标签:ble 数据库服务器 相关 语句 height var size while 提交
原文地址:https://www.cnblogs.com/furuihua/p/12172395.html