沃梦达 / 编程问答 / php问题 / 正文

默认获取为 OBJ 时的 stdClass

stdClass when fetch as OBJ by default(默认获取为 OBJ 时的 stdClass)

本文介绍了默认获取为 OBJ 时的 stdClass的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更喜欢使用 $obj->value 而不是 $array['value'],所以我使用了

I prefer use $obj->value than $array['value'], so i used

$database->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);

问题是当我做一个简单的请求时:

The problem is when i do a simple req :

 public function getUsers()
  {
    return $this->conn->query('SELECT * FROM users')->fetch();
  }

它返回给我一个 stdClass,我无法获取用户列表.(或者我不知道怎么做,但看起来很奇怪,我以前从未见过 stdClass).

It return me a stdClass and i can't get users list. (Or i don't know how, but it look weird, I never saw stdClass before).

所以有了 FETCH_OBJ 属性,我得到了:

So with FETCH_OBJ attribute I've got:

$list = $users->getUsers();
var_dump($list); // object(stdClass)#4 (6)[...]

没有 FETCH_OBJ 属性,我得到:(使用相同的代码)

Without FETCH_OBJ attribute I've got: (With same code)

array(12)[...]

我有 2 行 6 列,所以这一行给了我错误:(不是错误,但不是我想要的)

And I have 2 rows 6 cols so this line give me error: (not error, but not what I want)

$database->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);

你知道为什么吗?还有另一种方法可以将 FETCH_OBJ 设置为默认值吗?

Do you know why ? Is there another way to set FETCH_OBJ as default ?

我只想制作 fetch() 而不是 _fetch(PDO::FETCH_OBJ)_...

I just wanna make fetch() instead _fetch(PDO::FETCH_OBJ)_...

谢谢.

我不拥有数据库类.我正在这样做:数据库.php

I don't own database class. I'm doing like this: database.php

try {
    $database = new PDO('mysql:host=localhost;dbname=projet', 'root', '');
    $database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $database->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
  } catch(PDOExeption $e) {
    die ("error mysql: $e");
  }

UserManager 类:

UserManager class :

class UserManager
{
  private $conn;

  public function __construct(PDO $database)
  {
    $this->conn = $database;
  }

  public function getUsers()
  {
    return $this->conn->query('SELECT * FROM users')->fetch();
  }

}

在我想使用 getUsers() 的任何文件中:

And in any file i want to use getUsers():

$users = new UserManager($database);
$list = $users->getUsers();

推荐答案

StdClass 是 PHP 通用对象(如评论中所述).

StdClass is PHP generic object (as said in the comments).

我也会尝试回答您在评论中提出的其他问题.

I'll try to answer to other questions you ask in comments too.

您只得到一个结果,因为您使用的是 fetch,它给出了 下一个结果,当没有任何结果时返回 false.你有几个选择:

You're only getting one result because you are using fetch which give the next result, false when there isn't any result left. You have a few options for that :

  • 产量

使用生成器.这意味着您暂停"该方法,并且您必须像 Iterable 一样调用它.例如:

Using a generator. It means that you "pause" the method, and you have to call it like an Iterable. For example :

public function getUsers()
{
  yield $this->conn->query('SELECT * FROM users')->fetch();
}

会像这样使用:

foreach ($userManager->getUsers() as $user) {
    // $user is a row of your database
}

小心这个,因为它可以让你的数据库连接打开.

Be carefull with this because it can let your database connection open.

  • FetchAll

  • FetchAll

它会在一个数组中一次返回所有查询结果.请注意,如果您只有一个结果,您将拥有一个包含一个元素的数组.对于这个你会做的:

It returns all the results of your query at once in an array. Note that if you have only one result, you will have an array of one element. For this one you would do :

 public function getUsers()
 {
     return $this->conn->query('SELECT * FROM users')->fetchAll();
 }
 //...
 $users = $userManager->getUsers();
 foreach ($users as $user) {
     // your code
 }

另外,如果你想要一个你已经创建的用户类的实例,你可以使用 PDO::FETCH_CLASS.您将在此处找到文档.

Also not that if you want an instance of a User class you've made, you can use PDO::FETCH_CLASS. You will find the documentation here.

希望对你有帮助!

小编辑,如果不是很清楚,您可以将格式指定为 ::fetch() 参数,例如:

Small edit in case it's not quite clear, you can specify the format as ::fetch() argument such as :

public function getUsers()
{
    return $this->conn->query('SELECT * FROM users')->fetchAll(PDO::FETCH_ASSOC);
    // will return results as associative array even if you specified FETCH_OBJ in your constructor
}

这篇关于默认获取为 OBJ 时的 stdClass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:默认获取为 OBJ 时的 stdClass

基础教程推荐