alphacooler Posted June 27, 2006 Share Posted June 27, 2006 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! Quote Link to comment Share on other sites More sharing options...
thepip3r Posted June 27, 2006 Share Posted June 27, 2006 i'm assuming this is a column in a table in a database??? which database? if it's MySQL, use something like mysql_fetch_array() and RTFM. =D Quote Link to comment Share on other sites More sharing options...
alphacooler Posted June 27, 2006 Author Share Posted June 27, 2006 [!--quoteo(post=388642:date=Jun 27 2006, 03:57 PM:name=thepip3r)--][div class=\'quotetop\']QUOTE(thepip3r @ Jun 27 2006, 03:57 PM) [snapback]388642[/snapback][/div][div class=\'quotemain\'][!--quotec--]i'm assuming this is a column in a table in a database??? which database? if it's MySQL, use something like mysql_fetch_array() and RTFM. =D[/quote]mysql_fetch_array just creates an array with the data (in this case the username) in the first row. I need to combine all of the data from all of the rows into one array that can be searched. Quote Link to comment Share on other sites More sharing options...
thepip3r Posted June 27, 2006 Share Posted June 27, 2006 [code]$sql = "SELECT $column FROM $table";$result = mysql_query($sql);while ($row = mysql_fetch_array($result)) { echo $row['$column'];}[/code]...displays ALL columns for a specific query in a MySQL database and if you want to write those vars to an array to search:change "echo $row['$column']" to "$array[] = $row['$column']" Quote Link to comment Share on other sites More sharing options...
alphacooler Posted June 27, 2006 Author Share Posted June 27, 2006 [!--quoteo(post=388661:date=Jun 27 2006, 05:21 PM:name=thepip3r)--][div class=\'quotetop\']QUOTE(thepip3r @ Jun 27 2006, 05:21 PM) [snapback]388661[/snapback][/div][div class=\'quotemain\'][!--quotec--][code]$sql = "SELECT $column FROM $table";$result = mysql_query($sql);while ($row = mysql_fetch_array($result)) { echo $row['$column'];}[/code]...displays ALL columns for a specific query in a MySQL database and if you want to write those vars to an array to search:change "echo $row['$column']" to "$array[] = $row['$column']"[/quote]ThePip3r, thanks man could you tell me why this didn't work (which is what I had before)[code] $myarray=array(); for ($i=0; $i<$num_results; $i++) { $username=$row['username']; $myarray[]=$username; }[/code] Quote Link to comment Share on other sites More sharing options...
.josh Posted June 28, 2006 Share Posted June 28, 2006 that doesn't work because it's not inside the fetch array loop. all you are doing there is assigning the same information $num_results times to each array position. [code]$sql = "select * from table";$result = mysql_query($sql);while ($info = mysql_fetch_array($result)) { $blah[] = $info;}[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.