Consult per month

1

I have a table which shows a monthly condominium invoice, but I need to show the expenses that were recorded in the last month only in the database and not all the expenses that are, here is how the table is formed of the database

Here is the code I'm using to make the query but it shows me as an error Warning: mysqli_fetch_array () expects parameter 1 to be mysqli_result, boolean given

                       require_once('./conexion.php');
  $sql = "SELECT * FROM gasto_g WHERE created_at BETWEEN DATE_SUB(NOW(), INTERVAL 2 MONTH) AND NOW() ORDER BY id_gasto_g DESC;" ;
  $res = mysqli_query($conexion,$sql);
  while($resultado = mysqli_fetch_array($res)){
                        ?>   
 


 

    
                        <tr>
                            <td><?php echo $resultado['id_gasto_g']; ?></td>
                            <td><?php echo $resultado['descripcion']; ?></td>
                            <td><?php echo $resultado['monto']; ?></td>

                            <td>
    
asked by Anderson Rey 04.09.2017 в 21:06
source

2 answers

0

The query is syntactically correct, except that some field is badly referenced:

SELECT * FROM gasto_g 
WHERE created_at BETWEEN DATE_SUB(NOW(), INTERVAL 2 MONTH) AND NOW()
ORDER BY id_gasto_g DESC;

The problem you indicate:

  

mysqli_fetch_array () expects parameter 1 to be mysqli_result, boolean given, line X

It's because mysqli_fetch_array() is receiving a boleano instead of an object mysqli_result , the variable $res of your code is false since mysqli_query() Returns FALSE in case of error .

Return values mysqli_query ()

  

Return FALSE in case of error. If a query of type SELECT, SHOW,   DESCRIBE or EXPLAIN is successful, mysqli_query () will return an object   mysqli_result For other successful mysqli_query () queries   will return TRUE.

What can it be because mysqli_query fails?

  • The $conexion or link passed as the first argument is not valid or failed.
  • The provided query is invalid or failed.
  • The string of the query exceeds max_allowed_packet (very rare thing).
  • Check the first 2 points, I suggest the problems are in the connection or in the query.

        
    answered by 05.09.2017 / 00:09
    source
    0

    I think this error is when it occurs in the query, in this case in the construction of select , at the end you write ORDER BY Id but the id field does not exist, I do not know if you want to sort it by user_id or g_id_id, try to see ....

        
    answered by 04.09.2017 в 21:23