TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-18-2009, 07:51 PM   #1 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default 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.
captainmerton is offline  
Reply With Quote
Old 05-18-2009, 08:00 PM   #2 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

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

}
allworknoplay is offline  
Reply With Quote
Old 05-18-2009, 08:21 PM   #3 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

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.
captainmerton is offline  
Reply With Quote
Old 05-18-2009, 08:27 PM   #4 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by captainmerton View Post
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?
allworknoplay is offline  
Reply With Quote
Old 05-18-2009, 08:43 PM   #5 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

have a look at: http://uk3.php.net/mysql_fetch_object
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 05-18-2009, 09:00 PM   #6 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by sketchMedia View Post
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...
allworknoplay is offline  
Reply With Quote
Old 05-18-2009, 10:16 PM   #7 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

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.
Enfernikus is offline  
Reply With Quote
Old 05-18-2009, 10:17 PM   #8 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Enfernikus View Post
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!!!
allworknoplay is offline  
Reply With Quote
Old 05-19-2009, 08:31 AM   #9 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by allworknoplay View Post
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?
__________________
Tanax is offline  
Reply With Quote
Old 05-19-2009, 04:48 PM   #10 (permalink)
The Acquainted
 
captainmerton's Avatar
 
Join Date: May 2009
Posts: 178
Thanks: 9
captainmerton is on a distinguished road
Default

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.
captainmerton is offline  
Reply With Quote
Old 05-19-2009, 04:50 PM   #11 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
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...
allworknoplay is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Generating XML from a Mysql DB with PHP's DOM functions (part one) sketchMedia XML, XSLT, XPath, XQuery 10 03-05-2013 07:41 AM
SELECT statement -- list of fields in WHERE clause Dave MySQL & Databases 7 09-17-2008 11:36 AM
Generating XML from a Mysql DB with PHP's DOM functions (part Two) sketchMedia XML, XSLT, XPath, XQuery 7 08-20-2008 12:02 AM
mysql update statement sarmenhb Absolute Beginners 12 05-20-2008 01:46 AM
MySQL - Is there a better way to SELECT Acrylic Absolute Beginners 3 10-24-2007 03:39 PM


All times are GMT. The time now is 06:09 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design