stdClass when fetch as OBJ by default

2019-09-02 15:17发布

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条回答
太酷不给撩
2楼-- · 2019-09-02 15:46

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
}
查看更多
ゆ 、 Hurt°
3楼-- · 2019-09-02 15:55

You can use setFetchMode in the query object, like this $query->setFetchMode(PDO::FETCH_OBJ); And then just $users = $query->fetch(). More in the documentation.

查看更多
登录 后发表回答