Page 1 of 1
SELECT DISTINCT into a Drop Box question
Posted: Mon Jul 20, 2009 2:15 pm
by hwmetzger
Hello everyone,
The query I am running is this:
"SELECT DISTINCT weekending FROM reports"
When I run it directly in the database I get several results back:
[weekending]
|6/13/2009|
|6/6/2009|
...
That part works fine, what I have a question about is how would I get the above data into a drop box to be selected. When I try to return it back from the fuction that query is in I only get ([weekending] => 6/13/2009) back.
Thanks for the help in advance!
Re: SELECT DISTINCT into a Drop Box question
Posted: Mon Jul 20, 2009 3:09 pm
by spider.nick
hwmetzger wrote:Hello everyone,
The query I am running is this:
"SELECT DISTINCT weekending FROM reports"
When I run it directly in the database I get several results back:
[weekending]
|6/13/2009|
|6/6/2009|
...
That part works fine, what I have a question about is how would I get the above data into a drop box to be selected. When I try to return it back from the fuction that query is in I only get ([weekending] => 6/13/2009) back.
Thanks for the help in advance!
Post your code back on the forum; however, it sounds like you
are not doing something like:
Code: Select all
while( $row = mysql_fetch_array($result) )
Nick
Re: SELECT DISTINCT into a Drop Box question
Posted: Mon Jul 20, 2009 3:19 pm
by hwmetzger
Code: Select all
function get_list()
{
$sql = "SELECT DISTINCT weekending FROM report";
$result = mysql_query($sql);
$item = mysql_fetch_assoc($result);
return $item;
}
That is what I have so far, I have no idea where to go from there.
But this only returns 1 date instead of 2.
Re: SELECT DISTINCT into a Drop Box question
Posted: Mon Jul 20, 2009 3:25 pm
by califdon
hwmetzger wrote:Code: Select all
function get_list()
{
$sql = "SELECT DISTINCT weekending FROM report";
$result = mysql_query($sql);
$item = mysql_fetch_assoc($result);
return $item;
}
That is what I have so far, I have no idea where to go from there.
But this only returns 1 date instead of 2.
That's correct. The function mysql_fetch_assoc() always returns exactly ONE ROW. When you use it again against the same result, it returns THE NEXT ROW, etc. So you need to call it in a loop to get all the results, one at a time.
http://us.php.net/mysql_fetch_assoc
Re: SELECT DISTINCT into a Drop Box question
Posted: Mon Jul 20, 2009 3:49 pm
by hwmetzger
So would I do something like:
Code: Select all
function get_list()
{
$sql = "SELECT DISTINCT weekending FROM report";
$result = mysql_query($sql);
while ($item = mysql_fetch_assoc($result) )
return $item;
}
Re: SELECT DISTINCT into a Drop Box question
Posted: Mon Jul 20, 2009 3:51 pm
by spider.nick
hwmetzger wrote:So would I do something like:
Code: Select all
function get_list()
{
$sql = "SELECT DISTINCT weekending FROM report";
$result = mysql_query($sql);
while ($item = mysql_fetch_assoc($result) )
return $item;
}
Nope. Try it yourself [i.e. - apply the code to your project and see if it functions correctly], and *then* post back about problems.
Nick
Re: SELECT DISTINCT into a Drop Box question
Posted: Mon Jul 20, 2009 6:18 pm
by califdon
The manual reference I gave you has examples to do exactly what you're asking.
You are probably thinking, "Why don't these guys just give me the answer?" Let me tell you why we don't. If our purpose was to offer to code your job for you, there would be few, if any, volunteers. We do get satisfaction from TEACHING people how to do it themselves. It's like the old saying, "Give a man a fish and you feed him for a day. Teach him how to fish and you feed him for life."
Re: SELECT DISTINCT into a Drop Box question
Posted: Tue Jul 21, 2009 12:42 pm
by hwmetzger
I figgured it out,
http://us.php.net/mysql_fetch_assoc didn't help at all