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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:46:16+00:00 2026-06-09T13:46:16+00:00

Note: I’m a PHP beginner which is why the code below might be bad.

  • 0

Note: I’m a PHP beginner which is why the code below might be bad.

Hi. I’m using this code at the moment to select 9 unique random rows and it works fine.

$quCountRows = $database->query("SELECT * FROM approved")->rowCount();
$arrRandomPictures = array();
while (count($arrRandomPictures) < 9) {
    $randNumber = mt_rand(1, $quCountRows);
    if (!in_array($randNumber, $arrRandomPictures)) {
        $arrRandomPictures[] = $randNumber;
    }
}
$quRandomPicture1 = $database->query("SELECT picName,picType FROM approved WHERE id='".$arrRandomPictures[0]."'")->fetch(PDO::FETCH_ASSOC);
$quRandomPicture2 = $database->query("SELECT picName,picType FROM approved WHERE id='".$arrRandomPictures[1]."'")->fetch(PDO::FETCH_ASSOC);
$quRandomPicture3 = $database->query("SELECT picName,picType FROM approved WHERE id='".$arrRandomPictures[2]."'")->fetch(PDO::FETCH_ASSOC);
$quRandomPicture4 = $database->query("SELECT picName,picType FROM approved WHERE id='".$arrRandomPictures[3]."'")->fetch(PDO::FETCH_ASSOC);
$quRandomPicture5 = $database->query("SELECT picName,picType FROM approved WHERE id='".$arrRandomPictures[4]."'")->fetch(PDO::FETCH_ASSOC);
$quRandomPicture6 = $database->query("SELECT picName,picType FROM approved WHERE id='".$arrRandomPictures[5]."'")->fetch(PDO::FETCH_ASSOC);
$quRandomPicture7 = $database->query("SELECT picName,picType FROM approved WHERE id='".$arrRandomPictures[6]."'")->fetch(PDO::FETCH_ASSOC);
$quRandomPicture8 = $database->query("SELECT picName,picType FROM approved WHERE id='".$arrRandomPictures[7]."'")->fetch(PDO::FETCH_ASSOC);
$quRandomPicture9 = $database->query("SELECT picName,picType FROM approved WHERE id='".$arrRandomPictures[8]."'")->fetch(PDO::FETCH_ASSOC);

Although it may work I’m pretty sure the same results can be obtained with less queries. Which would hopefully result in the page loading quicker.

Any suggestions?


Here’s the better code. Thanks to Amber!

$quCountRows = $database->query("SELECT * FROM approved")->rowCount();
$arrRandomPictures = array();
while (count($arrRandomPictures) < 9) {
    $randNumber = mt_rand(1, $quCountRows);
    if (!in_array($randNumber, $arrRandomPictures)) {
        $arrRandomPictures[] = $randNumber;
    }
}
$quRandomPicture = $database->query("SELECT picName,picType FROM approved WHERE id IN (".implode(', ', $arrRandomPictures).")")->fetchAll(PDO::FETCH_ASSOC);
  • 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-09T13:46:18+00:00Added an answer on June 9, 2026 at 1:46 pm

    Use in instead of == and pass a list of IDs. That way you can get all of the results from one query rather than 9.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Note: This is not a question about what is the best way with code
(note: the_geom is a geometry value (TYPE: LINESTRING), in this case i random them
note: this question is related to PostGIS and Postgresql and is implemented with PHP
note: question title is change as discussed in this meta Q&A I'm using the
Note: This might seem like a Super User question at first, but please read
Note : The code in this question is part of deSleeper if you want
Note The question below was asked in 2008 about some code from 2003. As
Note, this might perhaps be best suited on serverfault.com, but since it is about
NOTE: I thought I was using bash, but /bin/sh is linked to /bin/dash, which
Note: Although at first similar, this is not a duplicate of Using Spring, JPA

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.