Jump to content

mysql_query to array


urtlking2jo

Recommended Posts

I've stored the options I would like to use in a drop down box on multiple pages in a mysql db table.  I am trying to run a query to retrieve those options (years in this case: 1990, 1991, 1992, etc.) and then store those options in an array so that I can output those options easily multiple times while only running the query once.  I'm not overly familiar with arrays however, and am running into difficulty.

My query is as follows, but I don't know where to go after that.  Any help would be appreciated!

$qry = "SELECT years FROM tbl_property WHERE login_name = '$fusername' AND room = '$room'";
$result = mysql_query($qry);
Link to comment
Share on other sites

[code]<?php
$qry = "SELECT years FROM tbl_property WHERE login_name = '$fusername' AND room = '$room'";
$result = mysql_query($qry);

$years = array();
while ($row = mysql_fetch_array($result)) {
    $years[] = $row['years'];
}

//Then to create a select field (you could make this a function)
echo "<select name=\"selectName\">";
foreach ($years as $year) { echo "<option value=\"".$year."\">".$year."</option>";
echo "<\select>";
?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.