Reputation: 3072
I have this weird situation that i have encountered for the first time and given my new status in PHP its even more confusing. In the query below the word "asked by" repeats more times than it should. The amount of times it repeats is related to how many entries its not suppose to show i guess.
Code:
$sql = "SELECT DISTINCT
allowed_permissions.post_id,
client_visit.client_reason,
accounts.full_name,
client_visit.type
from allowed_permissions
LEFT JOIN
client_visit
on allowed_permissions.post_id = client_visit.visit_id
LEFT JOIN
accounts
ON client_visit.system_id = accounts.system_id
where
allowed_permissions.allowed_to_view = '$uid'";
$result = mysql_query($sql);
$query = mysql_query($sql) or die ("Error: ".mysql_error());
if ($result == "") {
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if($rows == 0) {
} elseif($rows > 0) {
while($row = mysql_fetch_assoc($query)) {
$reason = $row['client_reason'];
$person = $row['full_name'];
//Here the asked word gets repeated...
echo"$reason asked by $person";
}
}
Upvotes: 1
Views: 607
Reputation: 2083
What's surprising is that only "asked" is getting repeated... "asked by" would seem more likely. You're getting a bunch of rows that don't contain a $reason or a $person so PHP is printing out the literal text and leaving out the variables. If that's the case, you could get around it by doing something like this:
if ( ! empty( $reason ) && ! empty( $person ) ) {
echo $reason . ' asked by ' . $person;
}
If that fixes it, you should still deal with the bigger problem in your query. It's clearly not quite right. It's hard to say how you should fix it without more context. I'm guessing you need to ditch the DISTINCT and implement a GROUP BY clause.
Upvotes: 1