-2
Sample table: employees

    +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+
    | EMPLOYEE_ID | FIRST_NAME  | LAST_NAME   | EMAIL    | PHONE_NUMBER       | title  |companyID
    +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+---------------+
    |         100 | Steven      | King        | SKING    | 515.123.4567       | IT_PROG | 1241
    |         101 | Neena       | Kochhar     | NKOCHHAR | 515.123.4568       | IT_PROG | 1241
    |         102 | Lex         | De Haan     | LDEHAAN  | 515.123.4569       | IT_PROG | 1241
    |         103 | Alexander   | Hunold      | AHUNOLD  | 590.423.4567       | IT_PROG | 1241
    |         104 | Bruce       | Ernst       | BERNST   | 590.423.4568       | FI_MGR  | 1242
    |         105 | David       | Austin      | DAUSTIN  | 590.423.4569       | FI_MGR  | 1242
    |         106 | Valli       | Pataballa   | VPATABAL | 590.423.4560       | FI_MGR  | 1242      
    |         107 | Diana       | Lorentz     | DLORENTZ | 590.423.5567       | IT_PROG | 1242     
    |         108 | Nancy       | Greenberg   | NGREENBE | 515.124.4569       | FI_ACCOUNT |1300     
    |         109 | Daniel      | Faviet      | DFAVIET  | 515.124.4169       | FI_ACCOUNT |1300    
    |         110 | John        | Chen        | JCHEN    | 515.124.4269       | FI_ACCOUNT |1300    
    |         
    |         +-------------+-------------+-------------+----------+--------------------+------------+------------+----------+----------------+------------+--

Now when I click the First Name of employees table a separate page will open, profile.php and I use this following code to extract the title

                      <?php
                            require_once 'tabconnect.php';
                                if (isset($_GET['id'])) {
                                    $id = $_GET['id'];
                                        $query1 = mysqli_query($connection,"SELECT employees.EMPLOYEE_ID,employees.companyID,cemployees.title FROM employees  where employees.EMPLOYEE_ID=$id");
                                            while ($row2 = mysqli_fetch_array($query1)) {
                        ?>
                                            <!-- Displaying Data Read From Database -->                                     
                                                <?php echo $row2['title']; ?>                                       

                        <?php
                                }
                                }
                        ?>

But I don't caclculate total employees of a particular companies, in profile.php page Example 1241 (companyID) have 4 employees. How I will do that?

  • 1
    Learn to use parameterized queries. – sticky bit May 23 '20 at 14:30
  • 1
    inner join the employees table with a SELECT count(*), companyID FROM employees GROUP BY employees and use companyID in the on claose and prepared statemenst with parameters see https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – nbk May 23 '20 at 14:42
  • What is cemployees – Strawberry May 23 '20 at 20:34

1 Answers1

-2

You can use below query for that return count of the company in companycount key

$query1 = mysqli_query($connection,"SELECT employees.EMPLOYEE_ID,employees.companyID,employees.title,(select count(*) from employees as emp where emp.companyID = employees.companyID ) as companycount FROM employees where employees.EMPLOYEE_ID=$id");