Jump to content

Create An Array Dynamically?


alphacooler

Recommended Posts

[!--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.
[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']"
[!--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]
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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.