How to differentiate 2 id within the same query

1

I have the following quey:

$query = "SELECT pedidos.*, users.id, users.nombre, users.email, users.username FROM pedidos  INNER JOIN users ON pedidos.usuario=users.idusuario WHERE (email LIKE '%$busqueda%' OR nombre LIKE '%$busqueda%' OR monto LIKE '%$busqueda%' OR usuario LIKE '%$busqueda%' OR nro_transf LIKE '%$busqueda%' OR ci_nro_cuenta LIKE '%$busqueda%' OR banco_emisor LIKE '%$busqueda%' OR banco_destino LIKE '%$busqueda%') ORDER BY id DESC LIMIT $init, $limit_end";


And I would like to know how I can get the users.id data since I need it for a function within the same while and I would not like to make another query for it.
I already have inside my while:

while($row = $c->fetch_array(MYSQLI_ASSOC))
                {

              $rowUser = $row['usuario'];
              $rowid = $row['id'];
              //$userid = $row['id.users'];

I have already taken the id from the table as $ row ['id']; and I need to assign the value to the variable $ userid with the value of the user id and I do not know how to do it .

    
asked by Jose M Herrera V 15.11.2018 в 16:50
source

4 answers

1

I recommend you use an alias in your query like this:

$query = "SELECT pedidos.*, users.id AS uid,...

And then you get it like this:

$userid = $row['uid'];
    
answered by 15.11.2018 / 16:55
source
2

Simply use an alias for that column and retrieve it using that alias. Look at the example. The conflicting column has been renamed to "user_id":

$query = "SELECT pedidos.*, users.id user_id, users.nombre, users.email, users.username FROM pedidos  INNER JOIN users ON pedidos.usuario=users.idusuario WHERE (email LIKE '%$busqueda%' OR nombre LIKE '%$busqueda%' OR monto LIKE '%$busqueda%' OR usuario LIKE '%$busqueda%' OR nro_transf LIKE '%$busqueda%' OR ci_nro_cuenta LIKE '%$busqueda%' OR banco_emisor LIKE '%$busqueda%' OR banco_destino LIKE '%$busqueda%') ORDER BY id DESC LIMIT $init, $limit_end";

$userid = $row['user_id'];
$rowUser = $row['usuario'];
$rowid = $row['id'];
    
answered by 15.11.2018 в 16:59
1

If there is an id field in the orders table, you will have problems which would suggest that you do the following:

$query = "SELECT pedidos.*, users.id as id_usuario, users.nombre, users.email, users.username FROM pedidos  INNER JOIN users ON pedidos.usuario=users.idusuario WHERE (email LIKE '%$busqueda%' OR nombre LIKE '%$busqueda%' OR monto LIKE '%$busqueda%' OR usuario LIKE '%$busqueda%' OR nro_transf LIKE '%$busqueda%' OR ci_nro_cuenta LIKE '%$busqueda%' OR banco_emisor LIKE '%$busqueda%' OR banco_destino LIKE '%$busqueda%') ORDER BY id DESC LIMIT $init, $limit_end";

and in your php:

while($row = $c->fetch_array(MYSQLI_ASSOC))
                {

              $rowUser = $row['usuario'];
              $rowid = $row['id_usuario'];
    
answered by 15.11.2018 в 16:57
1

With an alias you get it:

$query = "SELECT pedidos.*, users.id as userid, users.nombre, ...;

In the while:

$userid = $row['userid'];

Greetings

    
answered by 15.11.2018 в 16:58