Skip to content Skip to sidebar Skip to footer

How To Select Month From Sql Date In Column Where Month Is Currentmonth

I need to make a select statement that returns Voornaam, Tussenvoegsel, Achternaam from customer WHERE (I have a column in my database which is called Geboortedatum, in here I have

Solution 1:

Using the MONTH() and CURDATE() MySQL functions you can do

WHERE MONTH(Geboortedatum) = MONTH(CURDATE())

You may also want to add a YEAR() check

AND YEAR(Geboortedatum) = YEAR(CURDATE())

unless you want all data for this month over multiple years

Second Issue

You are only fetching ONE row from the resultset, you either need to loop over the n rows in the resultset OR use the fetch_all() method. In this case the fetch_all() seems like the simplest approach.

$conn = new mysqli("localhost", "root", "xxxx", "fca");
if (!$conn) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

if ($_POST['key'] == 'bijnaJarig') {
    
    $sql = $conn->query("SELECT Voornaam, Tussenvoegsel, Achternaam, Telefoonnummer, Email 
                        FROM customer 
                        WHERE MONTH(Geboortedatum) = MONTH(CURDATE())");
    $all_rows= $sql->fetch_all();

    echo json_encode($all_rows);
}

Post a Comment for "How To Select Month From Sql Date In Column Where Month Is Currentmonth"