Home > Enterprise >  Get data from another table by just comparing the id using php
Get data from another table by just comparing the id using php

Time:10-06

I want to get data from another table by just calling if the ID and data from another table is the same.

Table_1

Id  | name | lastname

1   | Fred | Moore

Table 2

Id  | table1_id 

1    |    1

I can already get and store the table 1 id to my table 2, but i want to echo the name and lastname from the table 1.

e.g. if table 2 table1_id is equal to Table_1 Id it will print the name and lastname .

CodePudding user response:

Try this query

select table_1.name, table_1.lastname
from table_1
left join on table_2 on table_1.id = table_2.table1_id

CodePudding user response:

<?php

$a = "Table_1";
$b = "Table_2 ";

$allItem = $cxn->query("SELECT * 
FROM $a  
INNER JOIN $b
ON $a.id = $b.id
WHERE $a.id  = 1);

$allItem = $allItem->fetchAll();

$lastName = $allItem["lastname"];
$name = $allItem["name"];

?>

Maybe it could work!

  • Related