Search code examples
phpmysqlmysql-num-rows

MySQL number of rows always returning false


I recognize this has been asked multiple times here and I've tried all exhaustible possible solutions. Might there be something inherently incorrect with my query as to why it would return false no matter if my query returns data?

$q_log_dates = "SELECT * FROM log_dates WHERE week_date ='" . $currentWeek . "'";
$q_log_dates_result = mysqli_query($connection, $q_log_dates);
$numResults = mysqli_num_rows($q_log_dates_result);

if ($numResults > 0) {
    echo "data";
} else {
    echo "no data";
}

Solution

  • Just use object-oriented MySQLi functions. In your case $q_log_dates_result is an object and you can use its properties.

    $q_log_dates_result = mysqli_query($connection, $q_log_dates); //-Procedural
    //OR $q_log_dates_result = $connection->query($q_log_dates);   //Object oriented
    if ($q_log_dates_result->num_rows > 0) {
        echo "data";
    } else {
        echo "no data";
    }