Why is my LIMIT not working in my query??

Posted on 2011-04-21
Last Modified: 2012-05-11
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

Question by:FairyBusiness
    LVL 38

    Expert Comment

    by:Aaron Tomosky
    I don't think you need the curlies around per_page

    Author Comment

    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
    LVL 24

    Accepted Solution

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

    $images = Image::find_all();


    Author Closing Comment

    Yep that did it. I had forgot about that. Thanks!!

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now