# Populate combobox from mySQL

**URL:** <https://forum.kirupa.com/t/populate-combobox-from-mysql/117758>\
**Category:** Uncategorized\
**Created:** [July 30, 2004, 1:26am UTC](https://forum.kirupa.com/t/populate-combobox-from-mysql/117758 "2004-07-30T01:26:23Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![The\_Vulcan](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/the_vulcan/32/145_2.png) [@The\_Vulcan](https://forum.kirupa.com/u/The_Vulcan)\
**Post date:** [July 30, 2004, 1:26am UTC](https://forum.kirupa.com/t/populate-combobox-from-mysql/117758/1 "2004-07-30T01:26:23Z")

</div>

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

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

```php
 $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:**

```php
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… 🙂

---

<div class="post-metadata">

**Author:** ![Jeff\_Wheeler](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@Jeff\_Wheeler](https://forum.kirupa.com/u/Jeff_Wheeler)\
**Post date:** [July 30, 2004, 1:52am UTC](https://forum.kirupa.com/t/populate-combobox-from-mysql/117758/2 "2004-07-30T01:52:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![The\_Vulcan](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/the_vulcan/32/145_2.png) [@The\_Vulcan](https://forum.kirupa.com/u/The_Vulcan)\
**Post date:** [July 30, 2004, 2:12am UTC](https://forum.kirupa.com/t/populate-combobox-from-mysql/117758/3 "2004-07-30T02:12:06Z")

</div>

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… 🙂

---

<div class="post-metadata">

**Author:** ![Jeff\_Wheeler](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@Jeff\_Wheeler](https://forum.kirupa.com/u/Jeff_Wheeler)\
**Post date:** [July 30, 2004, 2:13am UTC](https://forum.kirupa.com/t/populate-combobox-from-mysql/117758/4 "2004-07-30T02:13:43Z")

</div>

good, glad you know where the problem is 🙂

---

<div class="post-metadata">

**Author:** ![The\_Vulcan](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/the_vulcan/32/145_2.png) [@The\_Vulcan](https://forum.kirupa.com/u/The_Vulcan)\
**Post date:** [July 30, 2004, 5:07am UTC](https://forum.kirupa.com/t/populate-combobox-from-mysql/117758/5 "2004-07-30T05:07:41Z")

</div>

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

```php
 
$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

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

```

---

<div class="post-metadata">

**Author:** ![ol4pr0](https://avatars.discourse-cdn.com/v4/letter/o/5fc32e/32.png) [@ol4pr0](https://forum.kirupa.com/u/ol4pr0)\
**Post date:** [July 30, 2004, 5:10am UTC](https://forum.kirupa.com/t/populate-combobox-from-mysql/117758/6 "2004-07-30T05:10:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![The\_Vulcan](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/the_vulcan/32/145_2.png) [@The\_Vulcan](https://forum.kirupa.com/u/The_Vulcan)\
**Post date:** [July 30, 2004, 5:13am UTC](https://forum.kirupa.com/t/populate-combobox-from-mysql/117758/7 "2004-07-30T05:13:17Z")

</div>

> mysql\_num\_rows($allComments);

Ok this is where it’s getting 30 from…

I will have rewrite mine to include this…

---

<div class="post-metadata">

**Author:** ![The\_Vulcan](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/the_vulcan/32/145_2.png) [@The\_Vulcan](https://forum.kirupa.com/u/The_Vulcan)\
**Post date:** [July 30, 2004, 5:17am UTC](https://forum.kirupa.com/t/populate-combobox-from-mysql/117758/8 "2004-07-30T05:17:22Z")

</div>

> $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 ?

---

<div class="post-metadata">

**Author:** ![ol4pr0](https://avatars.discourse-cdn.com/v4/letter/o/5fc32e/32.png) [@ol4pr0](https://forum.kirupa.com/u/ol4pr0)\
**Post date:** [July 30, 2004, 5:36pm UTC](https://forum.kirupa.com/t/populate-combobox-from-mysql/117758/9 "2004-07-30T17:36:31Z")

</div>

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

```php

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

```

---

<div class="post-metadata">

**Author:** ![The\_Vulcan](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/the_vulcan/32/145_2.png) [@The\_Vulcan](https://forum.kirupa.com/u/The_Vulcan)\
**Post date:** [July 30, 2004, 11:24pm UTC](https://forum.kirupa.com/t/populate-combobox-from-mysql/117758/10 "2004-07-30T23:24:06Z")

</div>

Thanks guys…

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

---

<div class="post-metadata">

**Author:** ![The\_Vulcan](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/the_vulcan/32/145_2.png) [@The\_Vulcan](https://forum.kirupa.com/u/The_Vulcan)\
**Post date:** [July 31, 2004, 12:45am UTC](https://forum.kirupa.com/t/populate-combobox-from-mysql/117758/11 "2004-07-31T00:45:56Z")

</div>

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…

```php
 
<?
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 .'';} 
// ********************************************************************************************************?>

```

---

<div class="post-metadata">

**Author:** ![Jeff\_Wheeler](https://avatars.discourse-cdn.com/v4/letter/j/59ef9b/32.png) [@Jeff\_Wheeler](https://forum.kirupa.com/u/Jeff_Wheeler)\
**Post date:** [July 31, 2004, 1:14am UTC](https://forum.kirupa.com/t/populate-combobox-from-mysql/117758/12 "2004-07-31T01:14:27Z")

</div>

seems quite compact 🙂
