0
I’m trying to get data from two tables right at the beginning of the session when the user logs in, when I use the
SELECT * FROM usuarios, orders WHERE email = '$login' and senha= '$senha'
I can log in quietly, until then good, more when I modify the code to
$comando="SELECT * FROM usuarios INNER JOIN orders ON usuarios.id_usuario = orders.id_usuario WHERE email.usuarios='$login' and senha.usuarios='$senha'";
It only lets me log in if the user already has some Orders (request). What I am trying to do is as soon as the user logs in, also show the requests he has already made etc, which are data that are in another table. Someone can see my mistake?
Personally, I would still prefer to do two distinct queries, in different roles.
– Inkeliz
Now I can’t get some table data as Id_usuario
– user172019
Users who have Orders(requests) I can get the id_user table users, plus those who do not have Orders, it does not return me the id_user
– user172019
That’s because the
id_usuario
is present in both tables, is ambiguous, so if you try to catchid_usuario
it will pick up from any one. You have to specifyusuarios.id_usuario
(I no longer use PHP, I don’t remember if it was possible) or select with an alias (SELECT *, usuarios.id_usuario as id_usuario FROM ....
), this way you force thatid_usuario
be theusuarios.id_usuario
.– Inkeliz
Then it would be "SELECT usuarios.id_usuario as id_usuario * FROM usuarios LEFT JOIN Orders ON usuarios.id_usuario = Orders.id_usuario WHERE usuarios.email='$login' and usuarios.password='$password'" ?
– user172019
@Edvan, basically. But, the
*
has always come before (SELECT *, usuarios.id_usuario as id_usuario FROM ...
) and not (SELECT usuarios.id_usuario as id_usuario, * FROM ....
), or replace with the columns you are actually using (which is preferable).– Inkeliz
Lord God bless you :)
– user172019
For example if I have 2 requests from the same Id_usuario, have to make it return me all the requests made by the same Id_usuario, or I would have to use a new query with WHILE?
– user172019
Each order will be a line, you will have to access each one using the
mysqli_fetch
normally. You can make aforeach(mysqli_fetch_array($con, $result) as $row) {
// var_dump($row);
}
– Inkeliz
There is how I do this using the data I already have from the user’s SESSION, or I will have to make a new SQL query
– user172019
Forgive me if I’m being uncomfortable, I’m not very familiar with PHP
– user172019