0

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";
}
user1040259
  • 6,369
  • 13
  • 44
  • 62

3 Answers3

3

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";
}
sybear
  • 7,837
  • 1
  • 22
  • 38
  • 1
    If you are going to use Object Oriented PHP you should use it fully. – StackSlave Jun 24 '13 at 20:41
  • Meaning using $db = new mysqli(domain, username, password, database); on a different secure php page and include it on the one you're using. Then you can say $res = $db->query(query string here);. Also, you should $res->free(); your results after getting the $res->num_rows and the results, and $db->close(); your connection for better server side performance. – StackSlave Jun 24 '13 at 20:49
1

mysqli_query returns type of object, where mysql_num_rows expects parameter 1 to be resource. You cannot mix mysqli_* functions with mysql_* ones, they are not interchangeable.

I's highly recommended to drop using mysql_* ones and stick to the other libraries such as already mentioned MySQLi

Royal Bg
  • 6,988
  • 1
  • 18
  • 24
1

You are using

mysqli_query

, so for $numResults , you should use

mysqli_num_rows