Because a query is detected as a Boolean

1

I am a novice student of programming and I have an error in certain lines of code, the question is I do not know why the query takes the value of a Boolean and it gives me the following error.

  

Uncaught Error: Call to a member function fetch_assoc () on boolean in C: \ xampp \ htdocs \ gdlwebcamp \ index.php: 74

<?php
                        function multi_sql($numerocategoria) {


                              $sql = " INNER JOIN categoriaevento ";
                              $sql .= " ON eventos.id_CatEvento = categoriaevento.idCat ";
                              $sql .= " INNER JOIN invitados ";
                              $sql .= " ON eventos.idInv = invitados.idInvitado ";
                              $sql .= " AND eventos.id_CatEvento = $numerocategoria ";
                              $sql .= " ORDER BY idEvento LIMIT 2 ";

                              return $sql;
                          }

 try {
                                include_once 'includes/funciones/conexion.php';
                                $acentos = $conn -> query("SET NAMES 'utf8'");
                                $sql = " SELECT idEvento, nomEvento, fechaEvento, horaEvento, CatEvento, icono, nombreInvitado,
                                apellidoInvitado FROM eventos ";
                                $sql .= multi_sql(1);
                                $sql .= multi_sql(2);
                                $sql .= multi_sql(3);
                                $resultado = mysqli_query($conn, $sql);
                            } catch (\Exception $e) {
                                echo $e -> getMessage();
                            }

                        ?>

                      <?php
                      echo $resultado;
                      $resultado -> fetch_assoc(); ?>
                      <?php $conn -> multi_query($sql); ?>

                      <?php do {
                          $eventos = $conn -> store_result();
                          $row = $evento -> fetch_all(MYSQLI_ASSOC); ?>

                          <?php $i = 0 ?>
    
asked by Juan Manuel Ocampo 11.12.2018 в 06:21
source

2 answers

0

The method mysqli::query only allows to execute a single sql sentence, to execute multiple sentences we must use mysqli::multi_query , also the sentences have to end in ; to execute multiple queries.

In your case, before deciding to use mysqli::multi_query let's see which judgment is being built and check if it is the most appropriate:

After joining all the pieces we have something like this

SELECT idEvento, nomEvento, fechaEvento, horaEvento, CatEvento, icono, nombreInvitado, apellidoInvitado FROM eventos
INNER JOIN categoriaevento ON eventos.id_CatEvento = categoriaevento.idCat INNER JOIN invitados ON eventos.idInv = invitados.idInvitado AND eventos.id_CatEvento = 1 ORDER BY idEvento LIMIT 2
INNER JOIN categoriaevento ON eventos.id_CatEvento = categoriaevento.idCat INNER JOIN invitados ON eventos.idInv = invitados.idInvitado AND eventos.id_CatEvento = 2 ORDER BY idEvento LIMIT 2
INNER JOIN categoriaevento ON eventos.id_CatEvento = categoriaevento.idCat INNER JOIN invitados ON eventos.idInv = invitados.idInvitado AND eventos.id_CatEvento = 3 ORDER BY idEvento LIMIT 2

Obviously this is an invalid query syntactically, for multiple reasons.

I assume that the set of results we want is: All the guests next to their events and categories, where the event category (id_CatEvento) is 1, 2 or 3.

Given your query we can deduce

  • that we have 3 tables eventos , invitados and categoriaevento
  • that the table eventos has FK categoriaevent.idCat
  • that the table invitados has FK eventos.idInv

If so far I am right, and we want for example to get all the guests that exist for all events where the category is 1, 2 or 3

We can make the following query:

    /* seleccionamos los campos */
    SELECT invitados.*, eventos.*, categoriaevent.*
    /* indicamos la tabla */
    FROM invitados
    /* unimos los campos de otras tablas relacionadas*/
    INNER JOIN eventos ON invitados.idInvitado = eventos.idInv
    INNER JOIN categoriaevento ON eventos.id_CatEvento = categoriaevento.idCat
    /* establecemos las condiciones*/
    WHERE eventos.id_CatEvento = 1
       OR eventos.id_CatEvento = 2
       OR eventos.id_CatEvento = 3;

Notice that we only use 2 JOIN, one for each table from which we add fields, we also have a WHERE with operators OR one for each category of event we want from the table events.

At this point you have several options, the most logical and simple, would be to discard your function multi_query() that really does not contribute anything since there is nothing dynamic in the query and establish the query directly in a correct way.

Or modify your base sql and adapt your function multi_query() so that you add the conditions of the WHERE dynamically according to your needs.

    
answered by 11.12.2018 / 18:56
source
0

Some time ago I did not use mysqli, but did you try to extract the quotes from the query string?

$resultado = mysqli_query($conn, $sql);
    
answered by 11.12.2018 в 13:51