0

I am trying to do a select statement from my MySQL DB in PHP using PDO. I successfully create an array with the query results. But then, I want to filter out duplicate entries with the same date, thus creating an array of dates.

function grabUsefulDates() {
$sql = "SELECT DATETIME, pointcount FROM `fjbarrett`";
$sql_array = dbSelect($sql);
$datesarray = array();
$i = 0;
while($i < count($sql_array)) {
    if (count($datesarray) == 0) {
        $datesarray[] = datetimeConvertToDate($sql_array[$i]['DATETIME']);
        echo "rollone";
    } else {
        foreach ($datesarray as $f => $date) {
            if ($date !== datetimeConvertToDate($sql_array[$i]['DATETIME'])) {
                echo $date;
                echo " " . datetimeConvertToDate($sql_array[$i]['DATETIME']);
                echo "<br>";
                $datesarray[] = datetimeConvertToDate($sql_array[$i]['DATETIME']);
            }
        }
        }
        $i++;
    }
var_dump($datesarray);
}

This code will not run, but when I remove the "add to array" line within the foreach loop, it will run. Any suggestions?

1
  • Have you removed the "add to array" line from the post too? I see nothing resembling. Commented May 29, 2015 at 6:29

1 Answer 1

3

Simply use this query :

SELECT DISTINCT DATETIME, pointcount FROM `fjbarrett`;
Sign up to request clarification or add additional context in comments.

1 Comment

Definitely helped. Had no idea about the DISTINCT function in MySQL. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.