赣南脐橙

佰草伐

导航

使用mysql_fetch_object()函数获取结果集中一行作为对象

使用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()函数类似,只是有一点区别,前者返回的是一个对象而不是数组,该函数只通过字段名来访问数组,使用下面的格式获取结果集中行的元素值。

1

$row->col_name  //col_name为列名,$row代表结果集

例如,如果从某数据表中检索 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">&nbsp;

                       <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 {      //do...while 循环

    ?>

    <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));

?>

posted on 2020-01-09 17:00  佰草伐  阅读(398)  评论(0编辑  收藏  举报

自定义导航网站

php基础知识

Wood Furniture