Rumah  >  Artikel  >  pangkalan data  >  使用mysql_fetch_object()函数获取结果集中一行作为对象(PHP操作MySQL数据库的方法五)

使用mysql_fetch_object()函数获取结果集中一行作为对象(PHP操作MySQL数据库的方法五)

黄舟
黄舟asal
2017-04-20 13:47:383337semak imbas

使用mysql_fetch_object()函数获取结果集中一行作为对象(PHP操作MySQL数据库的方法五)

使用mysql_fetch_object()函数 同样可以获取差选结果集中的数据,跟上一篇文章中介绍的函数是类似的,下面我们通过同一个实例的不同方法了解这两个函数在使用上的区别。

在上一篇文章《使用mysql_fetch_array()获取数组结果集中的信息(PHP操作MySQL数据库的方法四)》中我们介绍了mysql_fetch_array()函数获取结果集,那么今天我们继续介绍获取结果集的函数mysql_fetch_object()函数。

首先我们看下该函数的语法格式如下:

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

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

例如,如果从某数据表中检索 id 和 name值,可以用$row->id 和 $row->name 访问行中的元素值。

注意:

本函数返回的字段也是区分大小写,这是初学者学习编程最容易忽视的问题。

下面的实例通过mysql_fetch_object()函数获取结果集中的数据信息,然后使用 echo语句从结果集中以“结果集->列名”的形式输出个字段所对应的图书信息。

具体步骤如下:

1.创建一个PHP动态页面,命名index.php,在index.php中添加一个表单,一个文本框以及一个提交按钮,具体代码如下:

<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。具体代码如下:

<?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()函数获取查询结果集中的数据,其返回的值为一个对象:

<?php
header("Content-Type:text/html; charset=utf-8");
$sql = mysql_query("select from tb_book");       //执行查询语句
$info = mysql_fetch_object($sql);                 //获取查询结果,返回值为数组
if($_POST[&#39;Submit&#39;]=="查询"){                    // 判断按钮的值是否为查询
    $txt_book = $_POST[&#39;txt_book&#39;];              //获取文本框提交的值
    $sql = mysql_query("select * from tb_book where bookname like &#39;%".trim($txt_book)."%&#39;");  //执行模糊查询
    $info = mysql_fetch_array($sql);             // 获取查询结果
}
?>

4.使用 do...while循环语句,“结果列->列名”的方式输出结果集中的图文信息,代码如下:

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

输出结果为:

38.png

该函数就介绍到这里,下一篇我们将介绍另外一个函数,是逐行获取结果集中的每条记录,具体请阅读《使用mysql_fetch_row()函数逐行获取结果集中的每条记录(PHP操作MySQL数据库的方法六)》!

Atas ialah kandungan terperinci 使用mysql_fetch_object()函数获取结果集中一行作为对象(PHP操作MySQL数据库的方法五). Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn