class / mysql_fetch_object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • websafe

    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_obj ect.

    $result = mysql_query("SE LECT * FROM sometable LIMIT 0,1");
    $row = mysql_fetch_obj ect($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

    --

  • websafe

    #2
    Re: class / mysql_fetch_obj ect

    websafe <please@dont.wr ite.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($ob j));
    for($i = 0 ; $i < count ($arr); $i++ ) {
    eval('$this->' . "{$arr[$i]} = '{$obj->$arr[$i]}';");
    }
    }
    }

    $result = mysql_query("SE LECT * FROM sometable LIMIT 0,1");
    $row = mysql_fetch_obj ect($result);
    $assigned = new MyClass($row);
    ?>

    --

    Comment

    Working...