|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2014-11-21 10:47 UTC] eglic dot csdn at gmail dot com
Description:
------------
I tought to improve fetch_object such as follow code is useful:
mysqli_result::fetch_object ([ string $class_name = "stdClass" [[, string $field_name_prefix = ''], array $params ]] )
example code :
$result = $mysqli->query("select id,name from user limit 1");
$user = $result->fetch_object('stdClass','__');
var_dump($user);
output :
object stdClass {
$__id = 1;
$__name = 'someone';
}
-----------------------------------
in addition , i have a question :
why call __constructor at the last in this method?
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 20:00:01 2025 UTC |
NOT stdClass. for example : class UserEntity { function __construct(){ $this->name = ''; // Default value from mysql schema $this->status = 1; // default value from mysql schema } public $name; public $status; } //////////////////////////////////// Scene 1: $u = new UserEntity(); $u->name = 'test'; $u->status = 0; $userFactory->Save($u); this is correct. //////////////////////////////////// Scene 2: $u = $result->fetch_object('UserEntity'); var_dump($u); output : $u->name is ''; $u->status is 1;