473,402 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

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 2332
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Sundial Services | last post by:
I recently brought an old application out of mothballs and launched it under PHP5, only to get this message: Fatal Error: Class '1' not found It was clearly somehow an /execution/ time...
2
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A,...
1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
2
by: Alucard | last post by:
Hi all. I am a newbie in PHP. Recently I found that there are mainly two ways to fetch mysql(or other DB) data: by object or by array. What is the main differences between them? Will the...
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
3
by: Paul Lautman | last post by:
Are there any benefits to using mysql_fetch_object() instead of mysql_fetch_assoc()?
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
3
by: Bob Bedford | last post by:
Hi all, wondering if does exists a mysql to html table function or class written in PHP. something like. createtable($query,fieldname1,fieldname2,fieldname3,); the last parameter would...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.