Query mysql_query does not return the column

Asked

Viewed 323 times

1

Greetings to everyone in the community. I’m going through a strange problem. When I select query syntax and paste in Phpmyadmin, the query returns all the fields I want. But when I put it in the code, it doesn’t return any value.

I have a table of users [users] that contains the company to which it is crowded by the [users] field. [emp] that is indexed with the table companies [sjy_companies] that contains a field that links to an economic group [sjy_companies]. [group] which is indexed to the group table [sjy_group].

I need to get a list of fields from the flag table [sjy_flags]. [flag] that are contained in the companies table [sjy_companies]. [flag] if these companies belong to the economic group [sjy_companies]. [group] to which the user belongs based on the companies [users] field. [emp].

I already tested the $user variable and it’s working.

I am trying to execute this code but the query does not return any value. Where is the error?

    $title = BD::conn()->prepare("SELECT `usuarios`.`usuario_id`, `sjy_grupo`.`id_grupo` FROM `sjy_grupo` INNER JOIN `sjy_empresas`  ON `sjy_grupo`.`id_grupo` = `sjy_empresas`.`grupo` INNER JOIN `usuarios` ON `sjy_empresas`.`id_empresa` = `usuarios`.`emp` WHERE `usuario_id` =  ? GROUP BY `usuarios`.`usuario_id`, `sjy_grupo`.`id_grupo` LIMIT 0, 1");

    $title->execute(array($usuario));
    $dados = $title->fetch();
    $grupo = $dados['id_grupo'];
    $query = "SELECT sjy_grupo.id_grupo, sjy_bandeira.id_bandeira, sjy_bandeira.bandeira
                    FROM sjy_bandeira INNER JOIN 
                    sjy_grupo INNER JOIN sjy_empresas ON sjy_grupo.id_grupo = sjy_empresas.grupo 
                    AND sjy_bandeira.id_bandeira = sjy_empresas.bandeira
                    WHERE id_grupo = $grupo
                    GROUP BY sjy_grupo.id_grupo, sjy_bandeira.id_bandeira, sjy_bandeira.bandeira";

    $result = mysql_query($query);
    while($fetch = mysql_fetch_row($result)){
        echo "<option value='?id=724&band=". $fetch[0] . "'>" . $fetch[2] . "</option>";
    }
  • 1

    Is mixing mysql_ (Old PHP API and discontinued) with mysqli (new php API), so it will never work, both Apis do not communicate.

1 answer

1


The mysql_query and mysql_fetch_array belong to Old API of PHP:

This old API, whose functions start with mysql_ are discontinued and were removed in PHP7.

The method you invoked BD::conn()->prepare() is probably mysqli->prepare which is part of NEW API calling for Mysqli PHP (another API new is the PDO).

To function mysql_ NAY communicate with the functions mysqli_ (and not in the OO style: mysqli->), that is there is no way your code works because it is mixing two different Apis.

Don’t use the old API

As I said at the beginning, functions that begin with mysql_xxxxx are old API functions, so do not use because they were removed in PHP7 and PHP5 it was already considered obsolete, if at the beginning of your code is using mysqli then just adjust everything to mysqli, if using the object-oriented style, then keep in it, recommend you read the documentation and see the examples:

Read more on:

Browser other questions tagged

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