I'm new here and this is my first question :)
I have this code:
<?php
/*
* Load messages from right users
*/
try {
$selectContactQuery = 'SELECT profile_id, user_id
FROM profilecontact
WHERE profile_id=:profile_id AND owner_id=:owner_id;';
$prepSelectContacts = $conn->prepare($selectContactQuery);
$prepSelectContacts->bindParam(':profile_id', $urlPid, PDO::PARAM_INT);
$prepSelectContacts->bindParam(':owner_id', $uid, PDO::PARAM_INT);
$prepSelectContacts->execute();
$contactsResult = $prepSelectContacts->fetchAll();
$contactsCount = count($contactsResult);
}
catch(PDOException $e) {
$conn = null;
header('Location: ../errors/error_101.html');
}
foreach($contactsResult AS $contactsRow) {
$scrapProfileId = $contactsRow['profile_id'];
$scrapUserId = $contactsRow['user_id'];
$selectScrapQuery = 'SELECT scraps.user_id, scraps.scrap_text, scraps.profile_id, scraps.add_date, user.user_id, user.profile_picture_50
FROM scraps
JOIN user ON(scraps.user_id = user.user_id)
WHERE scraps.user_id = scraps.user_id AND scraps.profile_id = scraps.profile_id ORDER BY add_date';
$prepSelectScrap = $conn->prepare($selectScrapQuery);
$prepSelectScrap->bindParam(':scraps.user_id', $scrapUserId, PDO::PARAM_INT);
$prepSelectScrap->bindParam(':profile_id', $scrapProfileId, PDO::PARAM_INT);
$prepSelectScrap->execute();
$scrapResult = $prepSelectScrap->fetchAll();
$scrapResultCount = count($scrapResult);
if($scrapResultCount > 0) {
foreach($scrapResult AS $scrapResultRow) {
echo '<div class="parentArrow"></div>
<div class="scrapItemParent">
<img class="scrapProfilePic" src=" ' . $scrapResultRow['profile_picture_50']. '" />
<div class="scrapContent">' . $scrapResultRow['scrap_text'] . '<br />
<span class="scrapTime">' . $scrapResultRow['add_date'] . '<br />' . $scrapResultRow['user_id'] . ' ' . $scrapResultRow['profile_id'] . '</span></div></div>';
}
}
}
?>
What I'm trying to do is with the first SQL query, I try to select the contacts of one user and a specific profile he/she is associated with. This gives me a resultset that looks like:
+-----------+---------+
|profile_id | user_id |
+-----------+---------+
| 32 | 45 |
| 32 | 56 |
| 32 | 78 |
+-----------+---------+
I get these results with a foreach, which you can see in my code starting on the line that says "foreach ($contactsResult AS $contactsRow)..." Within this foreach loop, I execute an SQL query to load messages from the users that are associated with the right profile (the first SQL query is to load the users and their associated profiles). However, I have multiple issues with this. First of all, in the second SQL query, there is the WHERE clause that says:
"WHERE scraps.user_id = scraps.user_id AND scraps.profile_id = scraps.profile_id"
However, the second WHERE condition, with scraps.profile_id... is being ignored. The messages are also being displayed multiple times (twice when I test it).
Does anyone know what's wrong with this code? It doesn't give me any errors, it just won't do what I want it to do. I know that most of these issues are because of the loops, but I can't see another way of achieving what I need.