[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

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??

http://auroriella.com/index.php
<?php
ini_set('display_errors' ,1); 
error_reporting(E_ALL);

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

find_selected_id();
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);
$database->query($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;
endforeach;

require 'footer.php';
?>

Open in new window

0
FairyBusiness
Asked:
FairyBusiness
  • 2
1 Solution
 
Aaron TomoskyTechnology ConsultantCommented:
I don't think you need the curlies around per_page
0
 
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
0
 
johanntagleCommented:
There's nothing wrong with your query.  However, in your code you disregard it and instead use:

$images = Image::find_all();

0
 
FairyBusinessAuthor Commented:
Yep that did it. I had forgot about that. Thanks!!
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now