Answer the question
In order to leave comments, you need to log in
How to correctly form a query to the database?
There are three tables:
users_app
contacts
messages
We get the user id as input to the script. From the database you need to get the contacts of this user, to these contacts from the users_app table, substitute information for each user (up to this point I did it using join),
$this->db->where('id_user', $id_user);
$this->db->join('users_app', 'users_app.id = contacts.id_contact');
$data = $this->db->get('contacts');
Answer the question
In order to leave comments, you need to log in
It's difficult without a dump, but try this request in PMA, instead of $id_user you just need to specify the real id
SELECT
a.*,
b.*,
c.*
FROM
`contacts` AS a
LEFT JOIN
`users_app` AS b
ON
b.id = a.id_contact
LEFT JOIN
`messages` AS c
ON
c.`id` =
(
SELECT
MAX(d.id)
FROM
`messages` AS d
WHERE
(
d.id_user = a.id_user
AND
d.id_from_user = a.id_contact
)
OR
(
d.id_user = a.id_contact
AND
d.id_from_user = a.id_user
)
)
WHERE
a.id_user = $id_user
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question