Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8503891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:52:46+00:00 2026-06-11T01:52:46+00:00

I have two tables, posts and sections. I want to get the last 10

  • 0

I have two tables, posts and sections. I want to get the last 10 posts WHERE section = 1,
but use the 10 results in different places. I make a function:

     function sectionposts($section_id){
mysql_set_charset('utf8');
$maxpost1 ="SELECT max(id) from posts WHERE section_id = $section_id ORDER BY ID DESC LIMIT 20";
$maxpost12 =mysql_query($maxpost1);
while ($maxpost_rows = mysql_fetch_array($maxpost12 ,MYSQL_BOTH)){
$maxpost2 = $maxpost_rows[0];
}
$query = "SELECT * FROM posts WHERE id = $maxpost2";
$query2 = mysql_query($query);
return $query2;
}
$query2 = sectionposts(6);
while ($rows = mysql_fetch_array($query2)){
echo $rows['title'] . "<br/>" . "<br/>";
echo $rows['id'] . "<br/>" . "<br/>";
echo $rows['image_section'] . "<br/>";
echo $rows['subject'] . "<br/>";
echo $rows['image_post'] . "<br/>";
}

How can it take these ten results but use them in different places, and keep them arranged from one to ten.

this was the old case and i solve it but i found another problem, that, if the client had deleted a post as id = 800 “so there aren’t id = 800 in DB” so when i get the max id minus $NUM from it, and this operation must be equal id = 800, so i have a programing mistake here, how can i take care of something like that.

    function getmax_id_with_minus ($minus){
    mysql_set_charset('utf8');
    $maxid ="SELECT max(id) FROM posts";
    $maxid1 =mysql_query($maxid);
        while ($maxid_row = mysql_fetch_array($maxid1)){
                $maxid_id = $maxid_row['0'];
                $maxid_minus = $maxid_id - $minus;
                }
    $selectedpost1 = "SELECT * FROM posts WHERE id = $maxid_minus";
    $query_selectedpost =mysql_query($selectedpost1);
        return ($query_selectedpost);

}
<?php 
$ss = getmax_id_with_minus (8);
while ($rows = mysql_fetch_assoc($ss)){
$main_post_1 = $rows;
?>

anyway “really” thanks again 🙂 !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:52:47+00:00Added an answer on June 11, 2026 at 1:52 am

    A few thoughts regarding posted code:

    • First and foremost, you should stop using mysql_ functions as they are being deprecated and are vulnerable to SQL injection.
    $maxpost1 ="SELECT max(id) from posts WHERE section_id = $section_id ORDER BY ID DESC LIMIT 20";
    
    • When you SELECT MAX, MIN, COUNT, AVG … functions that only return a single row, you do not need an ORDER BY or a LIMIT.

    • Given that you are only asking for the MAX(id), you can save work by combining your queries like so:

    SELECT * FROM posts 
    WHERE id = (SELECT MAX(id) from posts WHERE section_id = $section_id)
    

    If I’m understanding what you’re trying to do (please correct me if I’m wrong), your function would look something like:

    function sectionposts($section_id) {
        $link = mysqli_connect("localhost", "my_user", "my_password", "world");
    
        $stmt = mysqli_prepare($link, "SELECT title, id, image_section, subject, image_post FROM posts " 
            . "WHERE section_id = ? ORDER BY id DESC LIMIT 10");
        mysqli_stmt_bind_param($stmt, $section_id);
        return mysqli_query($link, $stmt)
    }
    
    $result = sectionposts(6);
    while ($row = mysqli_fetch_assoc($result)) {
        echo $rows['title'] . "<br /><br />";
        echo $rows['id'] . "<br /><br />";
        echo $rows['image_section'] . "<br />";
        echo $rows['subject'] . "<br />";
        echo $rows['image_post'] . "<br />";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got two tables: threads: id, title posts: id, thread_id, body I want
I have two tables called 'posts' and 'friends'. I want to display the my
I have two tables - forum_topics and topics_posts . I want to select rows
I have two tables, blogs and posts, with the expected relationship between them. Posts
I have two tables posts and comments . Table comments have post_id attribute. I
I have two tables Posts and comments Post Table Post_id Post_content Comments table comment_id
I have two MySQL (MyISAM) tables: Posts: PostID(primary key), post_text, post_date, etc. Comments: CommentID(primary
Okay so I have two tables, a category table and a posts table. I
I have two dependent tables as CREATE TABLE posts ( post_id int(11) unsigned NOT
I have two tables: posts - holds post information listen - holds information on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.