Hey all.

All I need is to collect every id from servers where active ='1' and then cycle the results in a loop right until the end for each individual id.

(I've written this on daniweb, might be typo's or errors.
I'm not interested in code errors it's the process itself)


$sql = mysql_query("SELECT `id` FROM `servers` WHERE `id` = '1'");
$ids = mysql_fetch_array($sql);

foreach ($ids){

// complete the process in these brackets


What I dont know is how you write the for each id do this part:

    foreach ($ids){

What do I need to use here to begin the process for each ID?

Thanks in advance!

Recommended Answers

All 4 Replies

Try this:

$list = array();
while($ids = mysql_fetch_array($sql)){
    $list[] = $ids['id'];



commented: good shout +1

I'll give it a shot now, thanks

I have nill experience with foreach. Is this right?

$sql = mysql_query("SELECT id from servers WHERE active = '1'");

       $list = array();

                while($ids = mysql_fetch_array($sql)){

                    $list[] = $ids['id'];
                    foreach($list as $id){

                    $get = mysql_query("SELECT * FROM SERVERS WHERE id = ".$id."");
                    $serv = mysql_fetch_array($get);

                    // rest of process here using $serv['cell_ref'] as a reference.


This should cycle through all rows. Is that right? (It's not working... so I'm hoping thats written wrong :P )

You can use a single query to do this and get directly the rows of the second query:

$sql = mysql_query("select * from servers, (select id as ids from servers where active = '1') as s where id = s.ids");

Then use the while loop as in my previous example to print/collect the result set:

while($row = mysql_fetch_array())
    echo $row['id'];
    # . . .
commented: true :D +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.