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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:18:51+00:00 2026-06-15T07:18:51+00:00

I would like to show 3 random images from database in my website. Below

  • 0

I would like to show 3 random images from database in my website. Below is its code:

$query = mysql_query ("SELECT id,url FROM tbl_gallery2");
if (mysql_num_rows($query) >= 3) {
    $my_array = array();
    $last_array = array();
    while ($r = mysql_fetch_row($query)) {
        $my_array[] = $r[1];
    }

    function makernd () {
        $number = array_rand($my_array,1);
        if (in_array($number,$last_array)) {
            makernd();
        } else {
            $last_array[] = $number;
            return $number;
        }
    }

    for($i = 1 ; $i < 3 ; $i++) {
        $item = makernd();
        echo '<img src="./images/slider/'.$item.'.jpg" alt="" class="slider" />';
    }

}

But whenever I run this code, I get the error below:

Undefined variable: my_array in line … // The first line of makernd() function.

But I expected $my_array to be an accessible array for this function.
What’s the problem?

  • 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-15T07:18:53+00:00Added an answer on June 15, 2026 at 7:18 am

    To simply fix your problem, you should pass $my_array to makernd() as a parameter:

    $query = mysql_query ("SELECT id,url FROM tbl_gallery2");
    if (mysql_num_rows($query) >= 3) {
        $my_array = array();
        $last_array = array();
        while ($r = mysql_fetch_row($query)) {
            $my_array[] = $r[1];
        }
    
        function makernd ($my_array) {
            $number = array_rand($my_array,1);
            if (in_array($number,$last_array)) {
                makernd($my_array);
            } else {
                $last_array[] = $number;
                return $number;
            }
        }
    
        for($i = 1 ; $i < 3 ; $i++) {
            $item = makernd($my_array);
            echo '<img src="./images/slider/'.$item.'.jpg" alt="" class="slider" />';
        }
    
    }
    

    HOWEVER, I strongly suggest putting the randomization in MySQL, to

    1. Simplify your code
    2. Significantly improve the performance, and
    3. Eliminate excessive loops & recursion in PHP

    Example:

    $sql = "SELECT id,url 
            FROM tbl_gallery2 
            ORDER BY RAND() 
            LIMIT 3";
    $query = mysql_query ($sql);
    if (mysql_num_rows($query) >= 3) {
        while ($r = mysql_fetch_row($query)) {
            echo '<img src="./images/slider/' . $r[1] . '.jpg" alt="" class="slider" />';
        }
    }
    

    PS – I also suggest you update your code to use mysqli, as mysql is deprecated

    PPS – I also suggest you look into mysqli_fetch_assoc so you can reference query results by name instead of index (e.g. $r['url'] instead of $r[1] – as if you ever change the order of your query, you will break references by index.

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

Sidebar

Related Questions

I want to show a random record from the database. I would like to
I would like to Show my Messagebox in the center of its parent form.
I would like to hide and show a number of buttons at random which
I would like to show a random image for each user on my system.
I would like to know why this code: import java.io.*; import java.util.Random; public class
I would like to show the value of a variable in a TextView, like
I would like to show and hide a div during hover and hover out.
I would like to show a div based on the Onclick event of an
I would like to show divs at a specific interval (10 seconds) and show
I would like to show different tabs in the top navigation of a SharePoint

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.