MYSQL into Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
stickman373
Forum Commoner
Posts: 30
Joined: Mon Jul 22, 2002 10:26 am

MYSQL into Array

Post 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?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
Last edited by McGruff on Thu Aug 11, 2005 2:06 pm, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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;
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Yes that's better: no point in declaring a var if you can avoid it.
Post Reply