Jump to content

Echoing SELECT DISTINCT in array


Daguse

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!
Link to comment
https://forums.phpfreaks.com/topic/9881-echoing-select-distinct-in-array/
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] .
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

Archived

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

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