Page 1 of 1

MYSQL into Array

Posted: Tue Mar 04, 2003 4:08 pm
by stickman373
Ok I make a query in mysql:

"SELECT * FROM ibf_moderators WHERE forum_id='".$class->forum['id']."'"

and then I want to load the each of the id column values from ibf_moderators in my $cc_array.

$cc_array = array();

while( $r = mysql_fetch_array($result2))
{
ADD VALUE into the array here
}

How do I loop through my results adding the id column into my $cc_array?

Posted: Tue Mar 04, 2003 9:46 pm
by McGruff

Code: Select all

$x=0;
while( $r = mysql_fetch_array($result2))  { 
   $cc_array[$x] = $r['ID_column_name']; 
   $x++;
}
PS: unless you need other columns for the table elsewhere in the script, it'll be a bit quicker to SELECT just the ID column rather than SELECT all.

Posted: Tue Mar 04, 2003 9:56 pm
by volka

Code: Select all

$cc_array = array(); // to avoid warnings
while( $r = mysql_fetch_array($result2))
   $cc_array[] = $r['ID_column_name'];
produces the same result, but of course you may save the hole row if you like

Code: Select all

$cc_array = array(); // to avoid warnings
while( $r = mysql_fetch_array($result2))
   $cc_array[] = $r;

Posted: Tue Mar 04, 2003 10:17 pm
by McGruff
Yes that's better: no point in declaring a var if you can avoid it.