SQL with PHP - SELECT problem

Asked

Viewed 48 times

0

I have an album, as soon as I click on the album, is to appear only the photos that are in the database with the id of the selected album, I click on the album and this coming the photos of the album with the selected id and the id of the other album too, come all photos in 1 album only, my select is like this:

select fotos.*, albuns.album_name from fotos 
INNER JOIN albuns ON fotos.foto_album = albuns.album_id

picked up the album_id variable by get from the page:

$album_id = $_GET['id'];
  • Interesting that you don’t even use the variable $album_id in the SQL clause. There should be no statement Where there indicating the id of the album?

  • That’s right, right now, it worked, thank you !

1 answer

1


You must use the WHERE.

SELECT fotos.*, 
       albuns.album_name 
FROM   fotos 
       INNER JOIN albuns 
               ON fotos.foto_album = albuns.album_id 
WHERE  albuns.album_id = 123456

In this case 123456 is the $album_id, for example:

$album_id = mysqli_real_escape_string($sql, $_GET['id']);

mysqli_query($sql, '    
    SELECT fotos.*, 
           albuns.album_name 
    FROM   fotos 
           INNER JOIN albuns 
                   ON fotos.foto_album = albuns.album_id 
    WHERE  albuns.album_id = "'.$album_id.'"
');

The WHERE will only get the data where the album_id of albuns is equal to the value of the $album_id.

  • Thank you, it worked perfectly !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.