Showing certain data depending on who logged in

1

I have a question regarding login for several people. I have this code:

<?php
    include("conectar.php");
    $usuario = $_POST['usuario'];
    $senha = $_POST['senha'];

    $query = "
        SELECT count(*)
        FROM usuarios
        WHERE usuario = '" . $usuario . "' AND senha = '" . $senha . "'";
    $consulta = mysql_query($query);
    $resultado = mysql_fetch_array($consulta);

    if ($resultado[0]) {
        echo "Login realizado com sucesso!";
    }
    else {
        echo "Dados incorretos.";
    }

?>

Each Login will have a specific data to display from a tb_company table. When I log in, I need to show this worker's data. I know it's necessary to do a query for each user. If the login is done by Y it does the query Y and it shows the data Y. If the login is made by Z it does the query Z and it shows the data Z. But I do not know how to do that.

The same data can be found in a MySQL database. The only data that can be related is ID_empresa and ID_usuario .

    
asked by anonymous 05.02.2015 / 16:09

1 answer

4

I have edited your SQL a bit according to the information passed by the comment and it looks like this:

(Do not forget to change the SQL for your case, the fields I placed fictitiously wondering how it would look in your database.)

  

Remember, when you use quotation marks ("test $ test data"), you do not need to break it to add a variable ("test." $ data. "test"), in this case PHP will understand that there is a variable somewhere and will replace it.

$usuario = 'teste';
$senha = 'teste2123';
$querySQL = "SELECT * FROM usuarios AS usu LEFT JOIN tb_empresa AS emp ON emp\.id_usuario = usu\.id WHERE usuario = '$usuario' AND senha = '$senha'";

var_dump($querySQL);
$query = mysql_query($querySQL);

if (!($row = mysql_fetch_array($query)))
{
    echo 'Usuário não encontrado!';
} else {
    echo 'Usuário encontrado!';
    var_dump($row); // Os dados estarão todos aqui, só utilizar como deseja!.
}

Test and report results;

    
05.02.2015 / 16:43