GuitarGod Posted October 20, 2008 Share Posted October 20, 2008 Is it possible to get results that are object oriented (if that's the right word ) from an SQL query. So instead of having something like $row['field_name'], you get $row->field_name? Link to comment https://forums.phpfreaks.com/topic/129244-object-oriented-results/ Share on other sites More sharing options...
F1Fan Posted October 20, 2008 Share Posted October 20, 2008 Not sure if there's a built-in way to do this, but you could create a class that would do it. Link to comment https://forums.phpfreaks.com/topic/129244-object-oriented-results/#findComment-670050 Share on other sites More sharing options...
Bendude14 Posted October 20, 2008 Share Posted October 20, 2008 well in your first example $row[field_name']; $row is an array and field name is the key for which item out of the array you want. your second example $row->field_name $row would usually be an object you have created of a class. and the field_name would be a method available to that class. So unless your field_name function retrieved an array of data from your DB i guess the answer is no Link to comment https://forums.phpfreaks.com/topic/129244-object-oriented-results/#findComment-670062 Share on other sites More sharing options...
Andy-H Posted October 20, 2008 Share Posted October 20, 2008 http://php.net/mysql_fetch_object Link to comment https://forums.phpfreaks.com/topic/129244-object-oriented-results/#findComment-670063 Share on other sites More sharing options...
F1Fan Posted October 20, 2008 Share Posted October 20, 2008 Um... uh... well... you should only use that one if you want to do it the obvious "easy" way... I think it'd be more fun to do it the hard way! Link to comment https://forums.phpfreaks.com/topic/129244-object-oriented-results/#findComment-670067 Share on other sites More sharing options...
Bendude14 Posted October 20, 2008 Share Posted October 20, 2008 lol learn something new every day Link to comment https://forums.phpfreaks.com/topic/129244-object-oriented-results/#findComment-670070 Share on other sites More sharing options...
rhodesa Posted October 20, 2008 Share Posted October 20, 2008 mysql_fetch_object is the way to go, but on a side note, PHP has a built in stdClass. so if you have an array want want to make an object out of it: <?php $array = array( 'foo' => 'bar', 'test' => 'value', ); $obj = new stdClass(); foreach($array as $k=>$v){ $obj->$k = $v; } echo $obj->foo; ?> Link to comment https://forums.phpfreaks.com/topic/129244-object-oriented-results/#findComment-670074 Share on other sites More sharing options...
trq Posted October 20, 2008 Share Posted October 20, 2008 Another side note. mysql_fetch_object can actually return a pre-defined object type. eg; <?php class user { public $uname; public function get_uc_uname() { return strtoupper($this->uname); } } $sql = "SELECT uname FROM users WHERE uid = 2"; if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { $user = mysql_fetch_object($result,'user'); echo $user->get_uc_uname(); } } ?> This can come in pretty handy. Link to comment https://forums.phpfreaks.com/topic/129244-object-oriented-results/#findComment-670141 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.