Page 1 of 1

[solved]select all rows's 'nick' fields

Posted: Thu Jan 15, 2004 10:26 am
by vigge89
How do i in MySQL return an array with the content of all the rows's 'nick' fields?
IE, a table looks like this:

Code: Select all

nick:      pass:        email:
nick1      testpass     some@thing
nick2      test         none
nick3      hello        test@domain.com
.......    .......      ..........................
and        so            on
I want to return an array with all the nicks, but i've got no idea on hwo to do it. i've looked at the MySQL functions on php.net, but I can't find a/the function that does this.....

Edit: is this only possible by using ForEaach, or While?

Posted: Thu Jan 15, 2004 11:44 am
by McGruff
Yes: loop through the db result resource (let's call that $query) with while:

Code: Select all

<?php

// intialise as an array
$all_the_nicks = array();

while($result = mysql_fetch_array($query))
{
    $all_the_nicks[] = $result['nick'];    
}

?>
Check out the manual for the mysql_fetch functions. Often they are pretty much interchangable.

Just in case you're not sure what $all_the_nicks[] means, this is a way of assigning new elements to an array. The (numerical) key is set +1 the last highest key.

You should always initialise a variable as an array before you start trying to add elements to it.

Posted: Thu Jan 15, 2004 11:53 am
by McGruff
Ooops I just noticed you've got 200-odd posts.. presumably you know how to loop through a db query.

In a sense a "SELECT nick FROM table" result resource is itself an array of nicks (which you could maybe just chuck around where it's needed) but, to create a php array, you'll have to do the standard loop & assign element thang.

Posted: Thu Jan 15, 2004 12:22 pm
by infolock
aye and then loop back through them to export them to be viewed :

Code: Select all

//...using mcgruff's script...
for($i=0; $i<count($all_the_nicks); $i++)
{
   echo 'Nick'.$i.' : '.$all_the_nicks[$i].' <br /><br />';
}
// sorry i couldn't resist

Posted: Thu Jan 15, 2004 2:44 pm
by vigge89
ok, so there wasn't any MySQL function that did the job for me :wink:
Just in case you're not sure what $all_the_nicks[] means, this is a way of assigning new elements to an array. The (numerical) key is set +1 the last highest key.
Well, i'm not a that big newbie :P