kristian
kristian

Reputation: 213

PHP/MySQL INNER JOIN Triples the amount of rows?

I have a system where I getting images out of my database, but when it does that, there is 3x of the same images.

I have tried with different ways, DISTINCT and such, but I have no clue how I fix this.

Here is my query code:

<?php
$id = $_GET['id'];

$query = "SELECT DISTINCT * FROM billeder INNER JOIN album ON fk_album_ID = $id";

$result = mysqli_query($con, $query);

while($row = mysqli_fetch_assoc($result))
{
    $thumb_src = 'billeder/thumb_'.$row['billeder_sti'];
    $full_src = 'billeder/'.$row['billeder_sti'];

    echo "
            <div class='ikon'>
                <a href='$full_src'>
                    <img src='$thumb_src' alt='' />
                </a>
            </div>
    ";
}

?>

Hope someone can help me on the way to fix this :)

Upvotes: 0

Views: 116

Answers (2)

Minoru
Minoru

Reputation: 1730

JOIN must be used with two tables columns. See example:

SELECT * FROM tableA a INNER JOIN tableB b ON a.id = b.a_id;

What you're trying to make is something like this:

"SELECT DISTINCT * FROM billeder INNER JOIN album ON 
    billeder.fk_album_ID = album.album_id WHERE billeder.id = $id"

You shouldn't pass an argument to the JOIN. The arguments must be used on the WHERE clause.

Upvotes: 0

AeroX
AeroX

Reputation: 3443

Without being able to see your table structure I won't be able to give an exact answer but the likely reason is because your INNER JOIN is not setup correctly.

SELECT DISTINCT *
FROM billeder
INNER JOIN album
    ON (billeder.fk_album_ID = album.pk_album_ID)
WHERE
    billeder.fk_album_ID = $id

Something like the above would be the correct way to JOIN a table and using a WHERE clause to then limit the date received.

Upvotes: 1

Related Questions