I can’t see the error (PHP and HTML)

Asked

Viewed 80 times

0

I wanted to show the names of the employees and the teams that they are part of so that if the user enters a name, it will filter all the teams that that person is part of. It’s coming in white.

   try{

                        $pdo = Conexao::getInstance();

                      $consulta = $pdo->prepare("SELECT id_equipe,nome_equipe,GROUP_CONCAT(nome_eletricista) as equipes FROM quadro WHERE id_agencia=:id AND status=1 GROUP BY nome_equipe ORDER BY nome_equipe ASC");

                        $consulta->bindParam(':id',$id_agencia, PDO::PARAM_INT);

                         if($consulta->execute()){

                          if($consulta->rowCount() > 0){

                          while($dados = $consulta->fetch(PDO::FETCH_OBJ)){

                                      $id_equipe= $dados['id_equipe'];
                                      $nome= $dados['nome_equipe'];
                                      $equipe= $dados->nome_equipe;
                                      $equipes=$dados->equipes;


                                      echo'<option value="'.$id_equipe.'">'.$nome.' - '.$equipes.'</option>';

                                          }
                                    }       
                                  }
                    }catch(PDOexception $e){

                        echo "ERROR: " . $e->getMessage();
                      }

                ?>
                              </select>
                              <br>
  • When you perform the query in the terminal or Phpmyadmin, it brings some result?

  • Oops! Gives an error: #1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'empresa_db.quadro.id_equipe' which is not functionally dependent on Columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

2 answers

0

disabling the sql_mode.

Do it like this:

mysql > SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FUL_GROUP_BY',''));

  • Oops! This script solves select in the database, but not in the application. And not showing all the form fields.

  • Is there a possibility to rollback this script? Because the screen was locked, after this bank discharge.

  • what version of mysql you are using?

  • 5.7.14 But I use the CONCAT function in another list and it works normally.

0


The server Mysql can operate in different modes SQL and can apply these modes differently to different clients, depending on the value of the variable sql_mode of the system.

The mistake only_full_group_by is generated when you use a non-aggregated value in a query who owns GROUP BY.

There are two ways to correct:

  • Using ANY_VALUE
$consulta = $pdo->prepare("SELECT ANY_VALUE(id_equipe), nome_equipe, ANY_VALUE(GROUP_CONCAT(nome_eletricista)) AS equipes FROM quadro WHERE id_agencia=:id AND status=1 GROUP BY nome_equipe ORDER BY nome_equipe ASC");
  • Disabling ONLY_FULL_GROUP_BY when instantiating the PDO object.
$pdo = new PDO("mysql:dbname=testdb;host=127.0.0.1", "root", "123456", [
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET sql_mode=(SELECT REPLACE(@@sql_mode,"ONLY_FULL_GROUP_BY",""));'
]);
  • Okay, I’ll try the query. But I have another problem, this script SET sql_mode=(SELECT REPLACE(@@sql_mode,"ONLY_FULL_GROUP_BY","");' Left my form screen locked. Know how to return to the previous state?

  • 1

    @Isadoraalmeida the problem is that you executed it so GLOBAL, this should only be done when you have - really - sure what you are doing. In case you want to do the rollback, just run: SET GLOBAL sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'. Ps: Back up before any global modification.

  • Not corrected. ( , but thank you

  • @Isadoraalmeida what didn’t fix? The query or the rollback?

  • I discovered here: the anyvalue that was giving problem. I was not recognizing. I thought it had been the global variable.

  • I haven’t been able to show the information in select yet. If you have any other ideas, thank you. I can go straight to the bank, but not to the app.

  • 1

    @Isadoraalmeida you came to try the second way, but using the select without the ANY_VALUE?

  • Oops! I managed to solve yes. The problem was DISTINCT.

  • Thank you so much for helping @Valdeir Psr

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.