TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Generating objects from a MySQL select statement (http://www.talkphp.com/absolute-beginners/4357-generating-objects-mysql-select-statement.html)

captainmerton 05-18-2009 07:51 PM

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.

allworknoplay 05-18-2009 08:00 PM

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

}

captainmerton 05-18-2009 08:21 PM

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.

allworknoplay 05-18-2009 08:27 PM

Quote:

Originally Posted by captainmerton (Post 24310)
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.

Oh I see, so you want to do this statically without creating any objects right?

sketchMedia 05-18-2009 08:43 PM

have a look at: http://uk3.php.net/mysql_fetch_object

allworknoplay 05-18-2009 09:00 PM

Quote:

Originally Posted by sketchMedia (Post 24315)

Is this a commonly used mysql function? I know I've seen it before but I know most people just use array or assoc.

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

Enfernikus 05-18-2009 10:16 PM

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.

allworknoplay 05-18-2009 10:17 PM

Quote:

Originally Posted by Enfernikus (Post 24332)
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.


Arrays all day!!!

Tanax 05-19-2009 08:31 AM

Quote:

Originally Posted by allworknoplay (Post 24316)
Is this a commonly used mysql function? I know I've seen it before but I know most people just use array or assoc.

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

Almost all MVC frameworks makes use of mysql_fetch_object as their "fetch"-method in their database class. Then they got extra methods for retrieving an array like "fetch_array"(but note that the main fetch method still uses the mysql_fetch_object).

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:

//using the mysql_driver class to set some base values:
$this->db->table('table')->where('row''value')->where('anotherrow'4)->order_by('row''ASC')->limit(10);
$query $this->db->get();
//$query will now be the mysql_result class and I can access methods like:
echo $query->num_rows();
foreach(
$query->fetch() as $row) {

print_r($row);

}
//etc. 

Is that what you're looking for?

captainmerton 05-19-2009 04:48 PM

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.

allworknoplay 05-19-2009 04:50 PM

Quote:

Originally Posted by Tanax (Post 24370)
Almost all MVC frameworks makes use of mysql_fetch_object as their "fetch"-method in their database class. Then they got extra methods for retrieving an array like "fetch_array"(but note that the main fetch method still uses the mysql_fetch_object).

Thanks Tanax, I will have to keep this in mind for the future...


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