Hello everyone please help me out regarding this query which is fetching the data from four tables. Is this the right way to write the query
SELECT task.employee_id , task.user_id , task.service_id from task
INNER JOIN employee employee.name , employee.pic ON employee.pno =employee_id
INNER JOIN user user.name , user.pic ON user.pno = user_id
INNER JOIN service service.name , service.description ON service.service_id =service_id";
and when i fetch the data how i will display them like we do $a = $data['id'];
and its coming with error
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result,
now i have updated to this
function viewAll()
{
$this->query = "SELECT task.employee_id , task.user_id , task.service_id, employee.name , employee.pic, user.name , user.pic, service.name , service.description
FROM task INNER JOIN employee ON employee.pno = task.employee_id INNER JOIN user ON user.pno = employee.user_id INNER JOIN service ON service.service_id = user.service_id;";
$rd = $this->executeQuery();
$recordsArray = array(); // create a variable to hold the informationm
while (($row = mysqli_fetch_array($rd)) ){
$recordsArray[] = $row; // add the row in to the results (data) array
}
and i am getting this error
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result,
No it's not correct.
SELECT task.employee_id , task.user_id , task.service_id, employee.name , employee.pic, user.name , user.pic, service.name , service.description
FROM
task
INNER JOIN employee ON employee.pno = task.employee_id
INNER JOIN user ON user.pno = employee.user_id
INNER JOIN service ON service.service_id = user.service_id;
I can't tell you what it should be really but that should be closer. I made my best guess at your joins and column names based on the original query. All fields you want to "see" need to be in the SELECT
clause. And when you are joining to another table you need to explain how. For instance, you are joining from task
to employee
so I assume that task
has a column called emmployee_id
(or similar) this is the field you need to join on.
Your query should resemble:
SELECT t.employee_id,
t.user_id,
t.service_id
FROM TASK t
JOIN EMPLOYEE e ON e.pno = t.employee_id
JOIN USER u ON u.pno = t.user_id
JOIN SERVICE s ON s.service_id = t.service_id
But with your query, there's a risk of duplicate rows because of the JOINs to EMPLOYEE, USER and SERVICE having a relating record in all of them, or one of the table having more than one related child record. Use:
SELECT DISTINCT
t.employee_id,
t.user_id,
t.service_id
FROM TASK t
JOIN EMPLOYEE e ON e.pno = t.employee_id
JOIN USER u ON u.pno = t.user_id
JOIN SERVICE s ON s.service_id = t.service_id
Another way of writing the query without using DISTINCT (or GROUP BY) is:
SELECT t.employee_id,
t.user_id,
t.service_id
FROM TASK t
WHERE EXISTS (SELECT NULL FROM EMPLOYEE e WHERE e.pno = t.employee_id)
AND EXISTS (SELECT NULL FROM USER u WHERE u.pno = t.user_id)
AND EXISTS (SELECT NULL FROM SERVICE s WHERE s.service_id = t.service_id)
$a = $data['id'];
Use the column name, or the column alias if defined, in the PHP code to get the appropriate value:
$a = $data['employee_id'];
$b = $data['user_id'];
$c = $data['service_id'];