Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>$department_id = 4; $result = mysql_query(' select l.* from leave as l join employee as e on l.emp_id = e.emp_id where e.dept_id = '.mysql_real_escape_string($department_id)); $department_name = 'this one'; $result = mysql_query(" select l.* from leave as l join employee as e on l.emp_id = e.emp_id join department as e on d.dept_id = e.dept_id where d.dept_name like '%".mysql_real_escape_string($department_name))."%'"); </code></pre> <hr> <p><strong>edit</strong></p> <p>After reading the first comment down there, I think you're saying that you essentially have an <code>employee_id</code> and you want to filter the query by that employee's department. So... here's some code to do that: </p> <p><a href="http://sqlfiddle.com/#!2/41bf7/1/0" rel="nofollow">http://sqlfiddle.com/#!2/41bf7/1/0</a></p> <p>There are two queries there... they're about the same so I would just choose which ever is easier for you to understand. You would add them to the PHP like this (using the first query from the sql fiddle): </p> <pre><code>$logged_in_employee_id = 1; $result = mysql_query('select e.emp_id, e.emp_name, l.date_apply, l.leave_type, l.status, d.dept_name, d.dept_desc from `leave` as l join employee as e on l.emp_id = e.emp_id join department as d on d.dept_id = e.dept_id where d.dept_id in ( select dd.dept_id from employee as ee join department as dd on dd.dept_id = ee.dept_id where ee.emp_id = '.mysql_real_escape_string($logged_in_employee_id)).' )'); </code></pre> <p>I'm not sure where you're getting the <code>employee_id</code> or the <code>department_id</code> but make sure you <strong>sanitize and validate</strong> anything you put into a query like this. I am using <a href="http://php.net/manual/en/function.mysql-real-escape-string.php" rel="nofollow"><code>mysql_real_escape_string</code></a> which helps but that query will still break if someone hijacks your <code>POST</code> data (or something) and uses a string instead of an integer value. There are some great posts on StackOverflow about how to do this; just search for sanitizing input, sql injection with PHP, and how to do prepared statements or use PDO. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload