Why is my LIMIT not working in my query??

Hi, I am trying to limit the number of images per page:

$per_page = 3;

$sql = "SELECT * FROM images ";
$sql .= "LIMIT {$per_page} ";
$sql .= "OFFSET {$page->offset()}";

But its displaying all of the images anyways.  What can I do to force the limit to work??

ini_set('display_errors' ,1); 

require_once 'includes/library.php';
$title = "Auroriella Management";
include 'header.php';

echo nav($select_nav);

include 'sidebar.php';
echo menu($select_menu, $select_product, $select_metal);

include 'display.php';
echo content_headers($select_menu, $select_product, $select_nav, $select_metal );

echo output($message);
// 1. the current page number
$page = 1;
// 2. records per page ($per_page)
$per_page = 3;
//3. total record count ($total_count)
$total_count = Image::count_all();

$page = new Page($page, $per_page, $total_count);
$sql = "SELECT * FROM images ";
$sql .= "LIMIT {$per_page} ";
$sql .= "OFFSET {$page->offset()}";
$image = Image::find_by_sql($sql);

$images = Image::find_all();

foreach($images as $image):
	echo "<a href=\"photo.php?id=" . $image->id . "\">";
	echo "<img src=\"" . $image->image_path() . "\" /></a>";
	echo $image->caption;

require 'footer.php';

Open in new window

Who is Participating?
There's nothing wrong with your query.  However, in your code you disregard it and instead use:

$images = Image::find_all();

Aaron TomoskySD-WAN SimplifiedCommented:
I don't think you need the curlies around per_page
FairyBusinessAuthor Commented:
I changed it but it doesn't matter:

$sql = "SELECT * FROM images ";
$sql .= "LIMIT " . $per_page;
$sql .= " OFFSET " .$page->offset();

Still it not limiting the images to 3 per page
FairyBusinessAuthor Commented:
Yep that did it. I had forgot about that. Thanks!!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.