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
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] .
Link to comment
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
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.