Query Question

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Can anyone advise me or point me to examples where I might figure out
how to populate the pulldown below automatically with a query when the
page is first opened?

I have a database with the dogs names in there that I would very much
like to populate through a db query of that table versus having to
hardcode the box when changes occur.  Database name=dogs and

<title>Dog Name Database Query</title>
<td valign=top align=center width=400>
<font color=white>.</font><br>
<font color=black face=ARIEL><p>
<form method="" action="">
<table border="1">
  <td>Dog Name:</td>
  <td><select name="dogname">
      <option value = "all">All</option>
        <option value = "spot">Spot</option>
        <option value = "snoopy">Snoopy</option>
        <option value = "oscar">Oscar</option>        etc...
  <td colspan="2" align="center"><input type="submit"

Re: Query Question

Something like this should suffice - assuming you are using MySQL



$conn = mysql_connect( "localhost", "<USERNAME>", "<PASSWORD>" );
mysql_select_db ( "dogs", $conn );

$sql = "SELECT * FROM dogs_tbl";
$rs = mysql_query( $sql, $connection );

$html = "<select name='dogname'>";
while($row = mysql_fetch_row($rs)) {
    $dogname = $row['dogname'];  //this assumes that the column name is
    $html .= "<option value='" .$dogname. "'>" .$dogname. "</option>";

$html .= "</select>";

Re: Query Question

Used some of the code you posted and got it working - thank you.   Is
there a way to keep duplicates out in a query?  If you had three
entrys for 'spot', five for 'oscar' and so on, how would you keep the
duplicates out of the query?

Also something I discovered, for the life of me, I couldn't make the
php query code run properly within an html TABLE document (even though
the extension is .php)

I have been using a table type html doc because it looks neater with
the three pulldowns within it.

thanks again for any help...


Re: Query Question

Quoted text here. Click to load it

Use keyword DISTINCT for that. It reduces duplicates to a single entry.

Welcome to Usenet! Please leave tolerance, understanding
and intelligence at the door. They aren't welcome here.

Re: Query Question

On Fri, 30 Sep 2005 12:40:35 +0300, "Kimmo Laine"

Quoted text here. Click to load it

Worked great - thanks.  Also, thanks ALL for emailed & posted replies.

This little project leaves me with one remaining question... So far, I
have a page that when loaded reads a MySQL database column and
populates a dropdown (pulldown) menu.  On that page there are input
options for a couple of other things such as the dog's age and owners
name so I end up with variables 'dogname', 'ownername', 'dogage'.

So I have the page loading fine and then I'll type into a text box,
ownername & dogage and click "enter" - for whatever reason, ownername
& dogage show up but the origninal 'dogname' variable apparently has
lost it's data or has not made it.

On my second page that shows what was input to the database, I'm using
$dogname = $_POST['dogname'];
$dogage = $_POST['dogage'];
$ownername = $_POST['ownername'];

with a print command to successfully echo the data entry results to
the screen but again, the original $dogname doesn't appear to make it
since the contents are blank.

After I've successfully queried the database for dogname, how do I
successfully pass along that data with dogage and ownername.

thanks very much for any help...

Site Timeline