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 8943061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:40:52+00:00 2026-06-15T11:40:52+00:00

Im trying to display a random record from my table but with no luck

  • 0

Im trying to display a random record from my table but with no luck this is what I have upto. This is coded in PHP

$range_result = mysql_query("SELECT MAX() AS max_id , MIN() AS min_id FROM pages");
$range_row = mysql_fetch_object( $range_result ); 
$random = mt_rand($range_row->min_id , $range_row->max_id);
$result_random = mysql_query( "SELECT * FROM pages WHERE id >= $random LIMIT 0,1");
echo $result_random;

Wondering if this is an obvious mistake? I have connected to the database fine.

JB

  • 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-15T11:40:54+00:00Added an answer on June 15, 2026 at 11:40 am

    You should do this with straight SQL with a mysqli_multi_query for performance:

    // First query gets the random ID, second gets the associated row
    $sql = "SELECT @id:=FLOOR(RAND()*(MAX(id) - MIN(id))) + MIN(id) FROM pages; 
            SELECT * FROM pages WHERE id = @id;"
    
    // Create a mysqli object and execute the multi-query
    $mysqli = new mysqli($host, $user, $pass, $db_name);
    $mysqli->multi_query($sql);
    $result = $mysqli->store_result();
    
    // Grab the first row from the first resultset
    if ($row = $result->fetch_assoc()) {
        print_r($row);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to display current time with PHP (using this ): $date = date('m/d/Y h:i:s
I’m trying to display a list of shops each with 3 random items from
Problem I am trying to display a random page from a file called ../health/
I have been trying to create a random picture display animation, in the style
I've trying to display random data from the database example in my database i
I am trying to display 10 random numbers from an array of numbers with
I am trying draw an random graph which display on imageview, I try this
trying to display data only after variables have been set. $(document).ready(function () { function
I am trying to display data that I am getting from server through json.
I am trying to display random banner each time the user refresh the page.

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.