Tuesday, 28 August 2018

The Mysqli query does not return any information?

Im attempting to find the next date. For what ever reason, the below code is not returning any information. I have 1 entry formatted as such : 2017-03-18 12:37:00.... what am I doing wrong?

<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'user');
define('DB_PASSWORD', 'pass');
define('DB_DATABASE', 'db');
$link = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
$query = "SELECT * FROM group WHERE date_time >= NOW() order by date_time LIMIT 1";
$next = mysqli_query($link, $query);
echo $next;
?>


You need to call a fetch function to get the data from the query. You should also check if it found anything; the fetch function will return null instead of an array if no rows were matched by the query.
$query = "SELECT * FROM `group` WHERE date_time >= NOW() ORDER BY date_time LIMIT 1";
$result = mysqli_query($link, $query);
$row = mysqli_fetch_assoc($result);
if ($row) {
    $date_time = $row['date_time'];
    echo "Next date is " . $date_time;
} else {
    echo "No time after now in the table.";
}

0 comments:

Post a Comment