views:

27

answers:

2

I have a PHP web application where users, when logged in, have a bunch of different properties associated with them in a MySQL table. I'd like an easy way to access and change (get/set) those properties.

What I want to do is something like this:

$obj = mysql_fetch_object(mysql_query("SELECT * FROM users WHERE userid = '$id'"));

So then, if I want change or access properties, I want to just do something like this:

$obj->password = "newpass";
echo $obj->password;

However, what I have now is that if I want to change the user's password, I have to do this:

mysql_query("UPDATE users SET password = 'newpass' WHERE userid = '$id'");

Is there an easier way to do this that takes up less code?

Thanks for any help in advance.

+1  A: 

I think the best (and possibly the most complex) way is to use an ORM. For PHP try Doctrine. An ORM will hide away the database details so you can just think of the user as an object.

speshak
Is that the extent of my options? I'm open to using an ORM, but is there any easier way to do what I want with PHP?
Chromium
ORM stands for object-relational-mapper, which is what you're sort of looking for. You don't necessarily have to use Doctrine, or any pre-made ORM for that matter. But the really do take care of what you're trying to do.
Matthew
Like Matthew said, you don't have to use Doctrine, you might want to roll your own. But your end goal really would be well served by some of the concepts of an ORM.
speshak
A: 

I found Idiorm- very simple, and easy to use.

Chromium