470,815 Members | 1,051 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

class / mysql_fetch_object

Hello :)
class MyClass {
var $ID;
var $NAME;
var $SIZE;
}

$blabla = new MyClass();

Then i have in a mysqldb a table with following fields
ID, NAME, SIZE

So :) Now i get a row from the table and fetch it using
mysql_fetch_object.

$result = mysql_query("SELECT * FROM sometable LIMIT 0,1");
$row = mysql_fetch_object($result);

As You can see :) $row has the same properties as $blabla.

My question: is there a way to 'convert' the object
$row to te class MyClass without doin' it like this

$blabla->ID = $row->ID;
$blabla->NAME = $row->NAME;
$blabla->SIZE = $row->SIZE;
I would like simply to assign the values of the properties of $row
to $blabla.
thanks for any help :)
websafe

--
http://www.partybitchez.org/
Jul 17 '05 #1
1 2258
websafe <pl****@dont.write.me> wrote:
Ok :) i've found a solution:

<?php
class MyClass {
var $ID;
var $NAME;
var $SIZE;

function MyClass($obj) {
$arr = array_keys(get_object_vars($obj));
for($i = 0 ; $i < count ($arr); $i++ ) {
eval('$this->' . "{$arr[$i]} = '{$obj->$arr[$i]}';");
}
}
}

$result = mysql_query("SELECT * FROM sometable LIMIT 0,1");
$row = mysql_fetch_object($result);
$assigned = new MyClass($row);
?>

--
http://www.partybitchez.org/
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Sundial Services | last post: by
2 posts views Thread by Fernando Rodriguez | last post: by
5 posts views Thread by Andy | last post: by
3 posts views Thread by Paul Lautman | last post: by
3 posts views Thread by Hamilton Woods | last post: by
3 posts views Thread by Bob Bedford | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.