Reputation: 1496
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.
Upvotes: 0
Views: 92
Reputation: 1372
try this out
$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 scraps.add_date';
$prepSelectScrap = $conn->prepare($selectScrapQuery);
$prepSelectScrap->bindParam(':scraps_user_id', $scrapUserId, PDO::PARAM_INT);
$prepSelectScrap->bindParam(':scraps_profile_id', $scrapProfileId, PDO::PARAM_INT);
Upvotes: 1
Reputation: 1392
You have to enter your php variables into the query
WHERE scraps.user_id = scraps.user_id AND scraps.profile_id = scraps.profile_id
This will allways equal true because scraps.user_id = scraps.user_id
is comparing something to itself
You can use:
WHERE scraps.user_id = :scrapUserId AND scraps.profile_id = :scrapProfileId
Here I've input the variables :scrapUserId
and :scrapProfileId
into your query.
This will enable you to set these values with these statements:
$prepSelectScrap->bindParam(':scrapUserId', $scrapUserId, PDO::PARAM_INT);
$prepSelectScrap->bindParam(':scrapProfileId', $scrapProfileId, PDO::PARAM_INT);
Upvotes: 1