-2

On my index page I have the "latest work" which shows 8 portfolio pieces and I need it to only grab the ones that have active = 1 as well and I have something like this:

$sql = 'SELECT * FROM portfolio WHERE active = 1';  

I tried doing this but its not working and i get errors when I try to pass it in PHPMyAdmin as well.

$sql = 'SELECT * FROM portfolio 
        WHERE active = 1 
        WHERE [id] > SELECT
        MAX([id]) - 8 FROM portfolio';

Any ideas?

Marc Delisle
  • 8,879
  • 3
  • 29
  • 29

3 Answers3

2

If you want to get the most recent 8 active pieces, use LIMIT as well as ORDER BY

$sql = 'SELECT * FROM portfolio WHERE active=1 ORDER BY id DESC LIMIT 8'
aynber
  • 22,380
  • 8
  • 50
  • 63
1
SELECT * FROM portfolio WHERE active=1 ORDER BY id DESC LIMIT 8
Arun Kumar M
  • 848
  • 9
  • 14
0
$sql = 'SELECT * FROM `portfolio` WHERE `active` = 1 ORDER BY `id` DESC LIMIT 8';

ORDER BY id DESC -> orders rows by highest to lowest value of ID, use ASC for opposite.

LIMIT 8 -> only 8 first rows

Lkopo
  • 4,798
  • 8
  • 35
  • 60