1
SELECT c.enddate FROM cohort c ORDER BY c.enddate DESC LIMIT 1

I have a sql query above, it works in database, the result select a date like: '2018-07-18'

I try to use while loop with mysqli_fetch_row in php to fetch this date, but the result will only fetch: 2018

How can I get the whole date?

if ($runquery = $conn->query($result_validation))
{
    //get the enddate from the last cohort
    while ($row = mysqli_fetch_row($runquery))
    {
        $lastDate = $row[0];
    }
}

$row[0] only display first number: 2018.

4
  • 4
    Can you please post the php code as well? It seems that's where the issue may lie. Commented Jul 9, 2018 at 0:28
  • Are you outputting the result in the loop or just trying to access after? Commented Jul 9, 2018 at 1:25
  • try adding DATE , SELECT DATE(c.enddate) FROM cohort c ORDER BY DATE(c.enddate) DESC LIMIT 1, if you can add your loop code here Commented Jul 9, 2018 at 1:26
  • 1
    @bdalina Put your code in comments to the answer so that the OP can mark the answer as accepted if your code works. Commented Jul 9, 2018 at 4:52

3 Answers 3

1

Adding DATE() function in the column will convert it to a valid date format like for the example below!

SELECT DATE(c.enddate) FROM cohort c ORDER BY DATE(c.enddate) DESC LIMIT 1
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for all of your answers, yes, simply add DATE() works
1

Try SELECT DATE(c.enddate) FROM cohort c ORDER BY c.enddate DESC LIMIT 1,

Comments

1

Try this one

SELECT DATE_FORMAT(c.enddate,"%Y-%m-%d") FROM cohort c ORDER BY c.enddate DESC LIMIT 1

The DATE_FORMAT() function formats a date as specified by a format mask.

Comments

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.