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 06-12-2008, 07:49 PM   #1 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default having nested mysql_fetch_assoc statements

is it possible to have something like this?
because in the nested query i am getting an error

the error says:

<b>Warning</b>: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in

this is the sql statement: select label from tbl_group order by id


Code:
$sql1 = "select * from table";
$sql2 = "select * from anothertable";

while($row1 = mysql_fetch_assoc($sql1)) { 

     // do something 

         while($row = mysql_fetch_assoc($sql2)) {


      // do something else

}
}
thnx
__________________
no signature set
sarmenhb is offline  
Reply With Quote
Old 06-12-2008, 07:56 PM   #2 (permalink)
The Contributor
 
buggabill's Avatar
 
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
buggabill is on a distinguished road
Default

You need to run mysql_query on those sql strings.

Like this:
php Code:
$sql1 = "select * from table";
$sql2 = "select * from anothertable";

$result1 = mysql_query($sql1);
$result2 = mysql_query($sql2);

while($row1 = mysql_fetch_assoc($result1)) {

     // do something

         while($row = mysql_fetch_assoc($result2)) {


      // do something else

}
}

mysql_fetch_assoc works on valid mysql result sets and not on strings that are intended to be queries.
__________________
-- Bill
"Why is it drug addicts and computer aficionados are both called users?" -Clifford Stoll
buggabill is offline  
Reply With Quote
Old 06-12-2008, 08:25 PM   #3 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

Plus, if you need to nest mysql_fetch_assoc functions, then you'll also need separate database connections ;)
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 06-13-2008, 05:06 AM   #4 (permalink)
The Contributor
 
dschreck's Avatar
 
Join Date: Nov 2007
Location: California
Posts: 82
Thanks: 0
dschreck is on a distinguished road
Default

Quote:
Originally Posted by xenon View Post
Plus, if you need to nest mysql_fetch_assoc functions, then you'll also need separate database connections ;)
He won't need to declare another mysql_connect(). MySQL will handle this by itself. While true, it does create another thread, it does not require to mysql_connect() again. Nesting does not truly have any adverse effects.
While it may be recommended that he does a JOIN instead of one query and another, or at least attempt a sub query, I wouldn't say using nested mysql_fetch_assoc() need any type of warning.

Just my 2 cents though :P
__________________
Where I Ramble: http://www.iwilldomybest.com/
What I do: Zynga Game Network
Senior Software Engineer at CityVille
dschreck is offline  
Reply With Quote
Old 06-13-2008, 06:35 AM   #5 (permalink)
Jim
The Addict
 
Jim's Avatar
 
Join Date: Nov 2007
Location: the Netherlands
Posts: 281
Thanks: 2
Jim is on a distinguished road
Default

It's the same database so multiple connections would be useless. When providing a different source (via a query) PHP will automagically differiate them. (is "differiate" english? )

I think a JOIN in the SQL would be a bit to much for this person at the moment. :) Not to diss him, but he's a beginner so this is enough already :)
__________________
Nunchaku! Who doesn't like martial arts? =)
Send a message via MSN to Jim Send a message via Skype™ to Jim
Jim is offline  
Reply With Quote
Old 06-13-2008, 04:12 PM   #6 (permalink)
The Contributor
 
dschreck's Avatar
 
Join Date: Nov 2007
Location: California
Posts: 82
Thanks: 0
dschreck is on a distinguished road
Default

Quote:
Originally Posted by Jim View Post
... (via a query) PHP will automagically differiate them. (is "differiate" english? )
...
I'll accept differiate, but automagically is just straight out silly ;)
__________________
Where I Ramble: http://www.iwilldomybest.com/
What I do: Zynga Game Network
Senior Software Engineer at CityVille
dschreck 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


All times are GMT. The time now is 03:59 PM.

 
     

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