0

I'm still new to programming especially in php and phpmyadmin and I have a quick question for a query I need to do in my php work, basically I want to compare the current date with the date which is in the expiration field of my table and get back all rows where the expiration field is greater than the current date.

here is my code:

function getnewsall(){
    $conn = connecte();

    $sql = "SELECT * FROM news WHERE CAST(expiration AS DATE) > CAST(GETDATE() AS DATE) ORDER BY publication";

    $result = mysqli_query($conn, $sql);

    $tab = mysqli_fetch_all($result, MYSQLI_ASSOC); 

    mysqli_free_result($result);

    mysqli_close($conn);

    return $tab;
}

I keep getting this error with all the different conditions I've tried enter image description here

Thank you in advance!

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Your `mysqli_query` is returning an error of some sort, and due to that fact, it's returning a bool value instead of the `mysqli_result` expected by the fetch method. Adding a `or die (mysqli_error($conn))` after your query statement might help you understand what's going on (like this: `$result = mysqli_query($conn, $sql) or die (mysqli_error($conn));` ) – Hotwer May 04 '22 at 18:59
  • What DB server are you using? `GETDATE()` doesn't exist in MySQL. – aynber May 04 '22 at 19:04
  • 1
    MySQL uses `NOW()` to get the current timestamp, or `CURDATE()` for the current date. No need to cast either one – aynber May 04 '22 at 19:07
  • Yeah I'm using MySQL, it said that GETDATE doesn't exist when I did die (mysqli_error($conn)); – LinksBurner May 04 '22 at 19:08
  • IT WORKED THANK YOU VERY MUCH, I'VE BEEN STUCK ON THIS FOR A WHILE – LinksBurner May 04 '22 at 19:11

0 Answers0