search
Homephp教程php手册深入分析使用mysql_fetch_object()以对象的形式返回查询结果

本篇文章是对使用mysql_fetch_object()以对象的形式返回查询结果进行了详细的分析介绍,需要的朋友参考下

mysql_fetch_object()同样用于获取查询数据结果集,香港虚拟主机,返回当前行数据,并自动滑向下一行。但与mysql_fetch_row()和mysql_fetch_array()不同的是,网站空间,它返回的是一个对象,这个对象的属性集合即为数据的属性集合,而属性上的值则为数据库中当前行该属性上的值。函数定义如下。

复制代码 代码如下:


object mysql_fetch_object( int result, int [result_type])


参数说明同mysql_fetch_array()。
返回值如下。
成功:一个对象,该对象的属性名对应于结果集中的属性名,属性的值就是结果集中对应的属性值。
失败:false。
下面是使用mysql_fetch_object()的示例:查询数据表Computers中的图书信息。

复制代码 代码如下:


1
2 3 //连接并选择到数据库服务器
4 $connection = mysql_connect ("localhost", "root", "password");
5 mysql_select_db("Books", $connection);
6 //查询数据
7 $query="SELECT * FROM Computers ";
8 $query.="WHERE price >= 20";
9 //echo $query."
";
10 $result=mysql_query($query,$connection);
11 //mysql_fetch_object()获得数据,并输出
12 while($row=mysql_fetch_object($result))
13 {
14 echo "书名: ".$row->name."
";
15 echo "价格: ".$row->price."
";
16 echo "出版日期: ".$row->publish_date."
";
17 echo "
";
18 }
19 ?>


示例在第12行使用mysql_fetch_object()获取当前行的数据,并通过12~18行的循环将之输出。在输出过程中,通过对象运算符“->”获得该行数据在其属性上的值。
示例运行的结果如下。

复制代码 代码如下:


书名: 数据结构
价格: 20
出版日期: 2001-01-01
书名: C语言
价格: 23
出版日期: 1998-04-04
书名: PHP入门技术
价格: 22
出版日期: 2005-05-01


,香港服务器
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor