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

  • Home
  • SEARCH
  • 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 8372435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:25:53+00:00 2026-06-09T14:25:53+00:00

I have have this query which fetches 2 random rows from a database: $query

  • 0

I have have this query which fetches 2 random rows from a database:

  $query  = "SELECT * FROM foo ORDER BY RAND() LIMIT 2";
  $result = mysql_query($query);

What I want is for the 2 choosen items to be remembered so they can be shown again on the next page, I had the idea this would be done by updating another table with the RAND() info but for the life of me cannot think how to do it nor can I find an info on how to do it.

Edit:
The overall idea is to show 2 new items as well as the last 2 items.

Edit 2:

Ok so it looks like it needs to be done using SESSIONS and I have come up with this so far but the results are the current items info rather then the previous

$item_array = array($item_id);
$_SESSION['lastitems'] = $item_array;

foreach($_SESSION['lastitems'] as $key=>$value) {
 echo $value . ' <br />';
}

the problem is I think is that $item_id is actually $row['itemid'] and can only be called from within the while loop hence the SESSIONS like I said before are the current items info rather then the previous how do I “store” so to show previous?

Edit 3: I stuck the above foreach outside the loop but it only returns 1 item

  • 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-09T14:25:54+00:00Added an answer on June 9, 2026 at 2:25 pm

    You can’t get the same order by rand() in two subsequent pages – that’s the whole point of ordering by rand in the first place.

    If you want to remember the data on the next page, why not stick the output into a $_SESSION and simply refer to it there?

    Having said that, you can seed the rand() function with a value along the lines of order by rand(3) which will give you the same results – but that opens up the can of worms where you will need to start passing the same seed to the other pages – which you will probably have to store in a $_SESSION so you end up at square one.

    Edit: If you want to add 2 new items, use your query as it is, and keep inserting the data into the $_SESSION variable. You can store arrays in there, so it should work a charm. Pull the data back, add it into the session, display all the results from the session. Next page, get two new results, add to session, display all the results from the session.

    Edit 2:

    Firstly, make sure you start the session off on each page:

    session_start();
    

    You want to treat the $_SESSION array just like any other array:

    $query  = "SELECT * FROM foo ORDER BY RAND() LIMIT 2";
    $result = mysql_query($query);
    

    Not sure how you iterate through your results before you output, so apply as you see fit:

    howeverYouLoopYouQueryResults
    {
        $_SESSION['lastitems'][]=$rowFromYourResult;
        // This appends the row to the end of the array
    }
    

    Now, when you get to displaying the data you can do something along the lines of:

    foreach($_SESSION['lastitems'] as $key=>$value) {
        echo $value . ' <br />';
        // Keep in mind that it will be storing ALL the 
        // items from the row because you used 'select *'
        // You can see what you selected with:
        print_r($_SESSION['lastitems'];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query which groups the results by ORDER#. SELECT ORDER#, MAX(SHIPDATE -
I have a query which looks like this: SELECT LossCost, CoverageID FROM BGILossCost] WHERE
I have this query which returns a result set with Date(column) as nvarchar datatype.
I have this NodeJS app which fetches JSON-type response from a web application and
I have an access report which fetches data from an underlying access query. The
I have a query such as this: select a.id, a.color, a.shade from colors a
I have this query which on executing in my sql command line client executes
I have this query which is a dependant query and taking much execution time
I have this query which works correctly in MySQL. More background on it here
Right now I have this SQL query which is valid but always times out:

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.