TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   having nested mysql_fetch_assoc statements (http://www.talkphp.com/absolute-beginners/2952-having-nested-mysql_fetch_assoc-statements.html)

sarmenhb 06-12-2008 07:49 PM

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

buggabill 06-12-2008 07:56 PM

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.

xenon 06-12-2008 08:25 PM

Plus, if you need to nest mysql_fetch_assoc functions, then you'll also need separate database connections ;)

dschreck 06-13-2008 05:06 AM

Quote:

Originally Posted by xenon (Post 15588)
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

Jim 06-13-2008 06:35 AM

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? :-P)

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 :)

dschreck 06-13-2008 04:12 PM

Quote:

Originally Posted by Jim (Post 15604)
... (via a query) PHP will automagically differiate them. (is "differiate" english? :-P)
...

I'll accept differiate, but automagically is just straight out silly ;)


All times are GMT. The time now is 01:37 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0