Jump to content

[SOLVED] Opinions on my way of using LIMIT


mike12255

Recommended Posts

So i had the following lines:

 

$page = $_GET['page'] * 10;
$tolimit = 15;
print "<table class='maintable'>";
print "<tr class='headline' color = 'white'><td width=20%>Author</td><td width=80%>Post</td></tr>";
$gettopic="SELECT * from forumtutorial_posts where postid='$id' AND area ='math' LIMIT $page,$tolimit"

 

My whole idea of using the limit feature was to limit the amount of posts displayed on a page. My friend told me that anything multiplied by something in php is an int so he said:

 

Apples * Oranges = 0

 

Which means that a string couldnt be entered protecting me from an sql injection. However I tried that with the url http://schoolworkanswers.com/message.php?id=22&catagory=math&page=1 however no posts showed up and im not sure why this happenings. Im guesing because i do Page 1 * 10 so its trying to start at the tenth post but i dont have a tenth post. Anyone have an opinion on how to change this to work better and still do what I want?

Link to comment
Share on other sites

I know i was talking to him about that and he said it wouldnt work for some reason

 

Sounds like your freind doesn't know what his talking about.

 

, and regardless my problem still wont be fixed using that

 

Well, you still need to check to see if your query finds any results within your limit. If it doesn't, you can at least display a nice error page.

Link to comment
Share on other sites

$page = ($_GET['page'] * 10)-10;
$tolimit = $_GET['page'] * 10;

 

if $_GET['page'] is 1, the records start from zero and go to the $tolimit which is ten more than $page.

 

That might work?

 

You don't want to multiply the $tolimit variable. The two varialbes for limit are the start index and the count. Using that logic page 1 would return the first 10 records, page two would return the next 20 records, page three would return the next 30 records, etc. Leave the $tolimit at 10 (or whatever count you want displayed on a page. In fact, it would be better to use a variable to determine the number of records per page so you can change it easily with one value:

 

$records_per_page = 10;
$page = (int) $_GET['page'];  //Convert to an int

//Determine total pages available
$query = "SELECT COUNT(*) FROM table";
$result  = mysql_query($query);
$record = mysql_fetch_row($result);
$total_pages = ceil($record[0] / $records_per_page);

if ($page < 1 || $page > $total_pages) { $page = 1 }
$limit_offset = ($page * $records_per_page) - $records_per_page;
$query = "SELECT * FROM table ORDER BY somefield LIMIT $offset, $records_per_page";

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.