user3456217 user3456217 - 7 months ago 15
PHP Question

mysql query for fetching these results

i have two tables in my database

1.Emp_ref_table with column

emp_id(pk)

emp_name

emp_address

dept_id

project_id


  1. Project_ref_table with column

    project_id(pk)

    project_name

    project_description



project_id is foreign key of emp_ref_table

my question is,if a user login with his emp_id then what will be the query to fetch result from these two tables like
this,

emp_id| emp_name |project_name|

Answer

The standard/safe way is this:

select e.emp_id, e.emp_name, p.project_name 
from Emp_ref_table e inner join Project_ref_table p 
  on p.project_id = e.project_id 
where e.emp_id = ?; 

you need to pass the ID of current user as parameter. This is one of the basics to prevent sql-injection

$preqry = "put your query here";
$stmt = $pdo->prepare($preqry);

$stmt->bindparam(1,$id_of_user);
$stmt->execute();