SELECT DISTINCT into a Drop Box question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hwmetzger
Forum Commoner
Posts: 25
Joined: Fri Jun 12, 2009 1:11 pm

SELECT DISTINCT into a Drop Box question

Post 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!
spider.nick
Forum Commoner
Posts: 72
Joined: Wed Jul 15, 2009 12:22 pm
Location: Overland Park, KS

Re: SELECT DISTINCT into a Drop Box question

Post 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
hwmetzger
Forum Commoner
Posts: 25
Joined: Fri Jun 12, 2009 1:11 pm

Re: SELECT DISTINCT into a Drop Box question

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: SELECT DISTINCT into a Drop Box question

Post 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
hwmetzger
Forum Commoner
Posts: 25
Joined: Fri Jun 12, 2009 1:11 pm

Re: SELECT DISTINCT into a Drop Box question

Post 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;
}
 
 
 
spider.nick
Forum Commoner
Posts: 72
Joined: Wed Jul 15, 2009 12:22 pm
Location: Overland Park, KS

Re: SELECT DISTINCT into a Drop Box question

Post 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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: SELECT DISTINCT into a Drop Box question

Post 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."
hwmetzger
Forum Commoner
Posts: 25
Joined: Fri Jun 12, 2009 1:11 pm

Re: SELECT DISTINCT into a Drop Box question

Post by hwmetzger »

I figgured it out, http://us.php.net/mysql_fetch_assoc didn't help at all
Post Reply