tags:

views:

39

answers:

3

hi,

here is what i'm trying to make. i want to select all the users from a table. for all of these users i want to make a for cycle.

from what i know for has three statements something like so for($i=0;$i++;$i=$max)

how can i put the user id's in to an array so i can define my $max variable?

or can this be done in an other method?

thanks,

+1  A: 

If you want to find the maximum id just run this query:

SELECT MAX(id) AS id FROM users

If you want to do it in PHP (for learning purpouses):

$ids    = array();
$result = mysql_query('SELECT * FROM users');

while ( $user = mysql_fetch_object($result) ) {
    $ids[] = $user->id;
}

$max = max($ids);
kemp
A: 

You could use:

mysql_num_rows($recordset)

This will give you the number of users from your table that you could use in your for loop.

You could also try:

    while ($i<mysql_num_rows($recordset)) {
      do something here
      $i++;
   }
simnom
Or am I barking up the wrong tree :-)
simnom
+1  A: 

I'm not sure I understand, or see, why you'd want to do it this way:

$res = mysql_query("SELECT id FROM users");
$max = mysql_num_rows($res);
$ids = array();
for ($i = 0; $i < $max; $i++) {
  $row = mysql_fetch_assoc($res);
  $ids[] = $row['id'];
}

When you can do it this way:

$res = mysql_query("SELECT id FROM users");
$ids = array();
while ($row = mysql_fetch_assoc($res)) {
  $ids[] = $row['id'];
}

But then again, I don't have much context.