douluan5444 2018-07-11 13:01
浏览 81
已采纳

默认情况下获取为OBJ时的stdClass

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

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

So with FETCH_OBJ attribute I've got:

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

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

array(12)[...]

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

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

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

Thanks.

Edit: 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 class :

class UserManager
{
  private $conn;

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

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

}

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

$users = new UserManager($database);
$list = $users->getUsers();
  • 写回答

2条回答 默认 最新

  • douyin2435 2018-07-11 13:31
    关注

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

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

    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 :

    • Yield

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

      Would be use like :

      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

      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
       }
      

    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.

    Hope it helps !

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.