0

I do not understand why mysqli_query return false, I seach a lot on internet but I have not found anything.

My code :

$M1 = "SET @Month = month(TIMESTAMPADD(month, -1, CAST(now() AS DATE)));    
Set @firstDay = CONCAT('01','/', @month, '/',year(now())); SET @lastDay = 
last_day(TIMESTAMPADD(month, -1, CAST(now() AS DATE))); SELECT count(*) 
AS M1 FROM APPEL where CAST(D_CREATION AS DATE) BETWEEN @firstDay and 
@lastDay";

$connexion = cnx();
if ($connexion) 
{
  $result = mysqli_query($connexion, $M1);
  if($result)
  {
    $row = mysqli_fetch_assoc($result); // This is Line 55
    $_SESSION['ArrayMois'][0] = implode($row);
  }
  else
  {
    $_SESSION['ArrayMois'][0] = 0;
  }   
}

The error is :

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in ... on line 55

This query always returns false but if I execute it in PHPMyAdmin I have one row returned.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Arthaiir
  • 23
  • 5
  • Does this answer your question? [mysqli\_fetch\_assoc() expects parameter / Call to a member function bind\_param() errors. How to get the actual mysql error and fix it?](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param) – Dharman Sep 22 '21 at 10:25

1 Answers1

1

1) MySQL reporting log locations

2) Read this answer for how to generate and read MySQL error reports.

For your code:

$row = mysqli_fetch_assoc($result) 
      or error_log("MYSQL Error on line ". __LINE__. 
         " :: ".print_r(mysqli_error($connection),true));

Update:

You state:

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in ... on line 55

What your error means:

It means the Query did not work as expected therefore:

$result = mysqli_query($connexion, $M1) 
          or error_log("MYSQL Error on line ". __LINE__. 
             " :: ".print_r(mysqli_error($connection),true));

The Issues:

You are running multiple separate SQL queries in one MySQL_query query handler, which by default will not be allowed in most instances. Maybe you have set up your SQL to accept concatenated querys, but generally that's unwise and less likely.

Alternative. You can create a PROCEDURE, FUNCTION or similar.

You are also not wrapping your column names in backticks, which is highly advisable.


Fxing your SQL:

Currently you have:

SET @Month = month(TIMESTAMPADD(month, -1, CAST(now() AS DATE)));  

Set @firstDay = CONCAT('01','/', @month, '/',year(now())); 

SET @lastDay = last_day(TIMESTAMPADD(month, -1, CAST(now() AS DATE))); 

SELECT count(*) AS M1 FROM APPEL where CAST(D_CREATION AS DATE) 
       BETWEEN @firstDay and @lastDay";

This can be written much more concisely as:

SELECT COUNT(*) as M1 FROM APPEL WHERE CAST(`D_CREATION` AS DATE) BETWEEN
(LAST_DAY(CURDATE() - INTERVAL 1 MONTH) + INTERVAL 1 DAY) AND LAST_DAY(CURDATE())

This will list all results between the first and last days of the current month. I am unclear from your code what you're exactly trying to reach but it appears you may want the previous month's listing, in which case you can use:

SELECT COUNT(*) as M1 FROM APPEL WHERE CAST(`D_CREATION` AS DATE) BETWEEN
(LAST_DAY(CURDATE() - INTERVAL 2 MONTH) + INTERVAL 1 DAY) 
AND LAST_DAY(CURDATE() - INTERVAL 1 MONTH)
Martin
  • 22,212
  • 11
  • 70
  • 132
  • Thank you, but if I use $M2 (same as $M1 but just for another month) mysql_query will return false, while he must normaly return true, because if I take the query and execute it in phpmyadmin I have one row returned. – Arthaiir Jun 18 '19 at 12:27
  • Thank you very much !! I understand why it did not work. And yes, I want the previous month's listing. – Arthaiir Jun 18 '19 at 12:50