Create an Array from a Query?

Discussion in 'PHP' started by alphacooler, Jun 27, 2006.

  1. #1
    I am querying one column in a table and would like to create an array that holds all of the data in the rows of that column. Say the column contains usernames. I would like to have one array that has every username in it.

    How would I create that array?

    Thanks!
     
    alphacooler, Jun 27, 2006 IP
  2. rws.com

    rws.com Banned

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you can use mysql_fetch_array() to do that
     
    rws.com, Jun 27, 2006 IP
  3. alphacooler

    alphacooler Peon

    Messages:
    70
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    mysql_fetch_array just creates an array with the data (in this case the username) in the first row right?. I need to combine all of the data from all of the rows into one SEARCHABLE array.
     
    alphacooler, Jun 27, 2006 IP
  4. shamess

    shamess Well-Known Member

    Messages:
    1,127
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    185
    #4
    $query = mysql_query("SELECT * FROM tablename");
    $array = mysql_fetch_array($query);

    $usernames = $array['usernames'];

    If that helps ^^;;
     
    shamess, Jun 27, 2006 IP
  5. shamess

    shamess Well-Known Member

    Messages:
    1,127
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    185
    #5
    If you want to echo each user name...

    while ($array = mysql_fetch_array($query)) {
    echo $username."<p>\n";
    }
     
    shamess, Jun 27, 2006 IP
  6. alphacooler

    alphacooler Peon

    Messages:
    70
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I don't think I was too clear before. I need to create an array like
    $array=("username1", "username2", "username3", "etc");
    Code (markup):
    that can be searched.
     
    alphacooler, Jun 27, 2006 IP
  7. shamess

    shamess Well-Known Member

    Messages:
    1,127
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    185
    #7
    Then you'll need to put it into an array that was first suggested and then you can search it with http://www.php.net/array_search]
     
    shamess, Jun 27, 2006 IP
  8. alphacooler

    alphacooler Peon

    Messages:
    70
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I found something that works:

     $myarray=array();
    while ($row=mysql_fetch_array($results)) {
    $username=$row['username'];
    $myarray[]=$username;
    } 
    Code (markup):
    I guess I was asking the wrong question?
     
    alphacooler, Jun 27, 2006 IP
  9. shamess

    shamess Well-Known Member

    Messages:
    1,127
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    185
    #9
    Well.. you've just made $row the exact same as $myarray (except its no longer quadratic.) But if that's the way you feel best doing it, then go ahead. ^^;;
     
    shamess, Jun 27, 2006 IP
  10. pl4y3r

    pl4y3r Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    But that's actually the only way to do it. Or is there any other way? ^^

    Greetings..
     
    pl4y3r, Jun 27, 2006 IP
  11. shamess

    shamess Well-Known Member

    Messages:
    1,127
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    185
    #11
    I guess understanding arrays is harder for some people than I thought >.< Lemme do an example...

    Here's an examlpe table called ... "table1".

    *table1*
    ------------------------------
    | username | userage |
    ------------------------------
    | bob | 12 |
    | shane | 16 |
    | dannycrane | 48 |
    | madona | 56 |
    -------------------------------

    (Yay for mock up tables)

    $query = mysql_query("SELECT * FROM table1"); //select all the rows
    $array = mysql_fetch_array($query) //put all the data into an array.

    The array now looks like this.
    array
    (
    username => bob, shane, dannycrane, madona
    userage => 12,16,48,56
    )

    So, if you wanted all the data from user name (the row field) you could use $array['username']. And that's all you need.

    So...

    while ($array2 = mysql_fetch_array($query)) { //you can't put an var that's already ben set to an array in a while like this
    echo $array2['username']."<p>";
    }

    Would echo...

    bob
    shane
    dannycrane
    madona

    Make sense? Probably not. Just stick with a way you know best ^^;;

    PS. I made lovely indents, but vB doesn't seem to like my whitespace ¬¬
     
    shamess, Jun 27, 2006 IP
  12. pl4y3r

    pl4y3r Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    but, as far as I know, mysql_fetch_array() only "fetches" the first row.
    Example (using your same table)

    $query = mysql_query("SELECT * FROM table1"); //select all the rows
    $array = mysql_fetch_array($query)

    $array would only contain

    username => Bob
    userage => 12

    Or is it not so? :p
     
    pl4y3r, Jun 27, 2006 IP
  13. shamess

    shamess Well-Known Member

    Messages:
    1,127
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    185
    #13
    Not if you put it into a while loop.
     
    shamess, Jun 27, 2006 IP
  14. pl4y3r

    pl4y3r Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Yes, of course ^^

    But the question is...

    Why wouldn't this code work:

    $myarray=array();
    while ($row=mysql_fetch_array($results)) {
    $username=$row['username'];
    $myarray[]=$username;
    }

    You said something about quadratic stuff... what did you mean? :p
     
    pl4y3r, Jun 27, 2006 IP
  15. shamess

    shamess Well-Known Member

    Messages:
    1,127
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    185
    #15
    Sorry for confusing you ^^;; That would work perfectly fine, but its a bit of a waste of a coding line.

    Quadratic arrays are those from a table (the results you get from mysql_fetch_array), which just show a lot more information in a more complicated way ^^;; Basically, it has more than more type of key that isn't just numbered 0,1,2,3,4,5,etc.
     
    shamess, Jun 27, 2006 IP
  16. pl4y3r

    pl4y3r Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    I understand... xD

    Hope the author of the topic didn't get confused... ^^
     
    pl4y3r, Jun 27, 2006 IP
  17. sadcox66

    sadcox66 Spirit Walker

    Messages:
    496
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Why not use SQL to do the search - it's good with that
     
    sadcox66, Jun 28, 2006 IP
  18. shamess

    shamess Well-Known Member

    Messages:
    1,127
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    185
    #18
    Say what now?
     
    shamess, Jun 28, 2006 IP