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

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

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

Post 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?
Last edited by vigge89 on Thu Jan 15, 2004 2:44 pm, edited 1 time in total.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
Last edited by McGruff on Wed Aug 10, 2005 12:00 am, edited 1 time in total.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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
Post Reply