Hướng dẫn fetchobject php - fetchobject php

Chuyển đến nội dung chính

Trình duyệt này không còn được hỗ trợ nữa.

Hãy nâng cấp lên Microsoft Edge để tận dụng các tính năng mới nhất, bản cập nhật bảo mật và hỗ trợ kỹ thuật.

PDOStatement::fetchObject

  • Bài viết
  • 09/09/2022
  • 2 phút để đọc

Trong bài viết này

Hướng dẫn fetchobject php - fetchobject php
Download PHP driver

Retrieves the next row as an object.

Syntax

  
mixed PDOStatement::fetchObject([ $class_name[,$ctor_args ]] )  

Parameters

$class_name: An optional string specifying the name of the class to create. The default is stdClass.

$ctor_args: An optional array with arguments to a custom class constructor.

Return Value

On success, returns an object with an instance of the class. Properties map to columns. Returns false on failure.

Remarks

Support for PDO was added in version 2.0 of the Microsoft Drivers for PHP for SQL Server.

Example

query( "select * from Person.ContactType where ContactTypeID < 5 " );  
   $result = $stmt->fetchObject();  
   print $result->Name;  
?>  

See Also

PDOStatement Class

PDO

Phản hồi

Gửi và xem ý kiến phản hồi dành cho

6 năm trước

Ẩn danh ¶Fetches the next row and returns it as an object

Vegard lkken ¶

8 năm trước PDOStatement::fetchObject(?string $class = "stdClass", array $constructorArgs = []): object|false

(Php 5> = 5.1.0, Php 7, Php 8, Pecl PDO> = 0.2.4)

Pdostatement :: fetchobject - tìm nạp hàng tiếp theo và trả về nó dưới dạng đối tượng

Sự mô tả

publicPdostatement :: fetchObject (? chuỗi $class = "stdClass", mảng $constructorArgs = []): object | false

Khi một đối tượng được tìm nạp, các thuộc tính của nó được gán từ các giá trị cột tương ứng và sau đó hàm tạo của nó được gọi.

Thông số

class

Tên của lớp được tạo.false on failure.

constructorArgs

9 năm trước

Be warned of the rather unorthodox behavior of PDOStatement::fetchObject() which injects property-values BEFORE invoking the constructor - in other words, if your class  initializes property-values to defaults in the constructor, you will be overwriting the values injected by fetchObject() !

A var_dump($this) in your __construct() method will reveal that property-values have been initialized prior to calling your constructor, so be careful.

For this reason, I strongly recommend hydrating your objects manually, after retrieving the data as an array, rather than trying to have PDO apply properties directly to your objects.

query( "select * from Person.ContactType where ContactTypeID < 5 " );  
   $result = $stmt->fetchObject();  
   print $result->Name;  
?>  
0

query( "select * from Person.ContactType where ContactTypeID < 5 " );  
   $result = $stmt->fetchObject();  
   print $result->Name;  
?>  
1

query( "select * from Person.ContactType where ContactTypeID < 5 " );  
   $result = $stmt->fetchObject();  
   print $result->Name;  
?>  
2

Val Bancer ¶

2 năm trước

query( "select * from Person.ContactType where ContactTypeID < 5 " );  
   $result = $stmt->fetchObject();  
   print $result->Name;  
?>  
3

Sebastian Dot Rapetti tại Alice Dot it ¶

5 năm trước

query( "select * from Person.ContactType where ContactTypeID < 5 " );  
   $result = $stmt->fetchObject();  
   print $result->Name;  
?>  
4

query( "select * from Person.ContactType where ContactTypeID < 5 " );  
   $result = $stmt->fetchObject();  
   print $result->Name;  
?>  
5

query( "select * from Person.ContactType where ContactTypeID < 5 " );  
   $result = $stmt->fetchObject();  
   print $result->Name;  
?>  
6

query( "select * from Person.ContactType where ContactTypeID < 5 " );  
   $result = $stmt->fetchObject();  
   print $result->Name;  
?>  
7

query( "select * from Person.ContactType where ContactTypeID < 5 " );  
   $result = $stmt->fetchObject();  
   print $result->Name;  
?>  
8

query( "select * from Person.ContactType where ContactTypeID < 5 " );  
   $result = $stmt->fetchObject();  
   print $result->Name;  
?>  
9

đang được

7 năm trước

$class0

$class1

$class2

$class3

Dave tại Davidhbrown dot chúng tôi ¶

7 năm trước

$class4

$class5

query( "select * from Person.ContactType where ContactTypeID < 5 " );  
   $result = $stmt->fetchObject();  
   print $result->Name;  
?>  
2

Dave tại Davidhbrown dot chúng tôi ¶

ZLK1214 tại Gmail Dot Com ¶

$class7

$class8

$class9

6 năm trước

7 năm trước

$constructorArgs0

$constructorArgs1

$constructorArgs2

$constructorArgs3

Dave tại Davidhbrown dot chúng tôi ¶

ZLK1214 tại Gmail Dot Com ¶

$constructorArgs4

$constructorArgs5

$constructorArgs6

$constructorArgs7