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!
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.
$query = mysql_query("SELECT * FROM tablename"); $array = mysql_fetch_array($query); $usernames = $array['usernames']; If that helps ^^;;
If you want to echo each user name... while ($array = mysql_fetch_array($query)) { echo $username."<p>\n"; }
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.
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]
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?
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. ^^;;
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 ¬¬
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?
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?
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.