![]() |
Generating objects from a MySQL select statement
How do I handle object generation when i'm calling a database to select several rows. Assuming I should instantiate each object or row as I retrieve them - would i create a static method in my class to read the rows from the table and when i read through them instantiate a class for each row?
Am i completely off the mark here. Have been reading a few OO PHP books but this isnt clear to me from them. |
Well I am no OOP pro, but you only instantiate your object once.
You then use that object to access your methods. So if you had a query method, your main script would be something like this: $object->query('SELECT * FROM users'); $rows = $object->results(); Then loop through to get your data: foreach($rows AS $data) { //code } |
I assume i wouldnt be instantiating an object if it was a static method. Not really understanding your explantion. If i have mysql table with 10 rows in it each row being one of my favourite films with each column storing various info on that film and i want to recall all rows for display on a page i would, for example, have the query "select * from films". How do i handle this from an OO perspective. Do i run the select then loop through th results instantiating an object for each row and calling a method to display the objects? I could stick the query in a static method within the class. Dunno if i'm making sense but i assume this is one of the most fundamental situations in OOP but i'm clueless any guidance appreciated.
|
Quote:
|
have a look at: http://uk3.php.net/mysql_fetch_object
|
Quote:
I see that it returns the results via object, with no relation to any class at all...and that you can only use names, not the ID's.... Is this really a useful function in everyday mysql use? Just wondering... |
This function is more useful when you know the data you'll be working with, which you usually always do therefore it comes down to wether you like working with Objects or Arrays in this case.
|
Quote:
Arrays all day!!! |
Quote:
Anyhow. @topic- I'm not sure what you want here? What I do in my database class is that every QUERY is a new object of a mysql_result class that I've built. PHP Code:
|
mysql_fetch_object - this more or less answers my question. Essentially i was asking how i instantiated an object for every row i was pulling back from a database and the only way i could think of doing it was to use a static method in the class with the mysql select in it and call that static function then instantiate an object of the class as a loop through each row upon recall. However this this fetch function does it all for me.
Thanks. |
Quote:
|
| All times are GMT. The time now is 07:27 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0