Jump to content


Photo

Echoing SELECT DISTINCT in array


  • Please log in to reply
2 replies to this topic

#1 Daguse

Daguse
  • Members
  • PipPip
  • Member
  • 26 posts

Posted 17 May 2006 - 09:05 PM

    $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>");

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!

#2 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 17 May 2006 - 09:15 PM

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] DISTINCT DATE_FORMAT(post_date, '%M %Y') AS formatted FROM table [!--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] .
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 17 May 2006 - 09:17 PM

Change your code to this:
$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>");

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users