Jump to content

Archived

This topic is now archived and is closed to further replies.

Daguse

Echoing SELECT DISTINCT in array

Recommended Posts

[code]    $query = "SELECT DISTINCT(DATE_FORMAT(post_date, '%M %Y')) FROM $tablename";
    
    if ($result = mysql_query ($query)) {
    while ($row = mysql_fetch_array ($result)) {
    echo("Date {$row['DATE_FORMAT']} <br>"); [/code]

So when I use the above code I don't get any thing from the date format just Date after each selection. Can any one inform me on why my echo is not working.... Thank you!

Share this post


Link to post
Share on other sites
if you run this query in the mysql command line or phpmyadmin, you'll see that the column returned for this query is "DATE_FORMAT(post_date, '%M %Y')", not just "DATE_FORMAT". One tip for you is to use mysql aliases.

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] [color=green]DISTINCT[/color] DATE_FORMAT(post_date, [color=red]'%M %Y'[/color]) [color=green]AS[/color] formatted [color=green]FROM[/color] [color=orange]table[/color] [!--sql2--][/div][!--sql3--]

Then, you can reference this column by $row['formatted']; If you're using fetch_array or fetch_row, you can also try $row[0] .

Share this post


Link to post
Share on other sites
Change your code to this:
[code]$query = "SELECT DISTINCT(DATE_FORMAT(post_date, '%M %Y')) FROM $tablename";
    
    if ($result = mysql_query ($query)) {
    while ($row = mysql_fetch_array ($result)) {
    echo("Date {$row['post_date']} <br>");[/code]

DATE_FORMAT is not the name of the row you are getting the data from, but post_date is

Share this post


Link to post
Share on other sites

×

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.