Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Re-using result variable
Old 03-27-2005, 04:51 PM Re-using result variable
Phaedrus's Avatar
Ultra Talker

Posts: 271
Location: CA
Trades: 0
I've always wondered about this, because it comes up often. In a script, I assign a result set to $result. Then I use $result to do something, then I need to run another query. Can I use $result again? Can I keep reusing it? Do I have to empty it first?

What I currently do is assign results to $result, then $result1, $result2, $result3, or sometimes $result_descriptivewordhere. But I don't really want to do this if it's not necessary.

Nothing critical, just something I've been wondering about.
__________________

Please login or register to view this content. Registration is FREE
Phaedrus is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-27-2005, 05:45 PM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Trades: 0
You can absolutely use it again. Like any variable, it can be reassigned a different value -- in this case, a result set from a database query.
__________________
—Kyrnt
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Old 03-27-2005, 06:36 PM
Christopher's Avatar
Iced Cap

Latest Blog Post:
Cross-domain AJAX with JSONP
Posts: 3,110
Location: Toronto, Ontario
Trades: 0
It's also worth noting that if your query is returning a very large result set, you should use mysql_free_result() to free the result set from memory. If the set is small, it shouldn't be a bother -- it will be cleaned at the end of the script's execution.
__________________

Please login or register to view this content. Registration is FREE
- Latest Articles:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

--
Please login or register to view this content. Registration is FREE

Christopher is offline
Reply With Quote
View Public Profile
 
Old 03-28-2005, 01:35 AM
Phaedrus's Avatar
Ultra Talker

Posts: 271
Location: CA
Trades: 0
Thanks!
__________________

Please login or register to view this content. Registration is FREE
Phaedrus is offline
Reply With Quote
View Public Profile
 
Old 03-28-2005, 07:40 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
As long as the queries are sequential, you can re-use the variable as much as you want. Watch out for nested loops though - say you loop over $result, and then re-use it inside the loop. That will overwrite the old value, which you still need for the loop. In that case, you would need to use $result2 for the inner query. Once the loop finished you can reuse both again. I've done this myself and it's a tough one to spot.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Reply     « Reply to Re-using result variable
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.22470 seconds with 12 queries