Populate combobox from mySQL

I am trying to populate a combobox with usernames pulled from mySQL.

This is the [color=seagreen]PHP [/color]code:

 $sql = 'SELECT * FROM `' . $users . '`'; 
 
 while($array = mysql_fetch_array($username); $i++){
 print "&writename$i="; $name = mysql_result($fewComments, $i, 'name'); print''. $name .'';}

This is the [color=red]Flash[/color] code:

i=0;
while (this["writename"+i] != undefined){
names.addItemAt(i,this["writename"+i]);
i++;}

When I test it all I get is one entry that says.
“This is a entry”

Any help or directions to tutes on this would be appreciated… :slight_smile:

are you sure that sql query returns everything correctly, try doing a print of the array

bugger it’s not…

I had copied the .fla and .php from another one that I knew worked and just added those bits…

I didn’t even consider that it wasn’t recieving data… I will get that working… :slight_smile:

good, glad you know where the problem is :slight_smile:

Ok I have it working now but I need work on my mySQL query skills.

 
$sql = 'SELECT name FROM `' . $table . '`';
 $allnames = mysql_query($sql, $DBConn) or die("Error in GuestBook Application: " . mysql_error());  
for($i=0; $i<=$allnames ; $i++){
 print "&named$i="; $name = mysql_result($allnames, $i, 'name'); print''. $name .'';

This is returning only the first 6 names… When I expected it to return all 30.

When I use $numallComments (defined before) it returns all 30 names…
So while I have it working, I don’t understand why the above returned only 6 entries.

This is the script that defines $numallComments

 
$sql = 'SELECT * FROM `' . $table . '`';  
   $allComments = mysql_query($sql, $DBConn) or die("Error in GuestBook Application: " . mysql_error());
   $numallComments = mysql_num_rows($allComments);

why not just use $row[‘username’] ?

mysql_num_rows($allComments);

Ok this is where it’s getting 30 from…

I will have rewrite mine to include this…

$row[‘username’]

Hey old…

My sql skills are very limited at the moment and I am using this PHP/mySQL project to learn along the way…

How would I use $row[‘username’] in my above script ?

you already done it a couple of times… as i remember.


// query
echo '<select name="namelistbox">';
while($row = mysql_fetch_assoc($result))
{
echo '<option>'.$row['username'].'</option>';
}
echo '</select>';

Thanks guys…

This project has been a bigger head ache and taking longer than I thought…

Well this is the php file I ended up with… No doubt it could be done better, but it’s the best I could do and it works…

 
<?
include 'gb_db.php';
// *******************************************************************************************************
 $sql = 'SELECT name FROM `' . $table . '`';
 $allComments = mysql_query($sql, $DBConn) or die("Error in GuestBook Application: " . mysql_error());
 $numallComments = mysql_num_rows($allComments);
 $allnames = mysql_query($sql, $DBConn) or die("Error in GuestBook Application: " . mysql_error());
// ********************************************************************************************************  
 for($i=0; $i<$numallComments ; $i++){
 print "&named$i="; $name = mysql_result($allnames, $i, 'name'); print''. $name .'';} 
// ********************************************************************************************************
?>

seems quite compact :slight_smile: