I want to insert the returned values into an array and after to display random string
I have this code by it doesn't return back a random value

Inside the vid1,vid2,vid3 columsn there is Video Name text that pointing to the video in my File Manager folder

$sql = "SELECT vid1, vid2, vid3 FROM table WHERE ID = 2";
$result = $conn->query($sql);
if(!$result) {
    echo "Something went wrong, please try again.";
} else {
    if($result->num_rows == 0){
        echo "Something went wrong, please try again.";
        exit();
    } else if($result->num_rows >= 1) {
        while($row = $result->fetch_assoc()) {
            $vid1 = $row['vid1'];
            $vid2 = $row['vid2'];
            $vid3 = $row['vid3'];
        }
    }
}

$videos  = array(
    $vid1,
    $vid2,
    $vid3
);

$video_to_play = $videos[array_rand($videos)];

And When i hardcode it like this it works. but i am not selecting from the database here.

$videos  = array(
        'video/202212060125.mp4',
        'video/202212060148.mp4',
        'video/202212060152.mp4'
    );

$video_to_play = $videos[array_rand($videos)];

It looks like you're trying to select some videos from a database and then choose one at random to display. The code you've provided seems to be almost correct, but there are a few things you can change to make it work better.

First, when you're building your array of videos, you're using the $videos variable to store the list of videos, but then you're overwriting that same variable with a single random video later on. Instead, you should use a different variable name for your array of videos, such as $video_list, and then choose a random video from that array.

Next, it's generally a good idea to check if the query was successful before trying to access the results. You can do this by checking the num_rows property of the $result object, which tells you how many rows were returned by the query. If num_rows is 0, then the query didn't return any rows and you should display an error message.

Here's how you could modify your code to address these issues:

$sql = "SELECT vid1, vid2, vid3 FROM table WHERE ID = 2";
$result = $conn->query($sql);
if(!$result) {
    echo "Something went wrong, please try again.";
    exit();
} else if($result->num_rows == 0) {
    echo "No videos were found, please try again.";
    exit();
} else {
    $video_list = array();
    while($row = $result->fetch_assoc()) {
        $video_list[] = $row['vid1'];
        $video_list[] = $row['vid2'];
        $video_list[] = $row['vid3'];
    }
    $video_to_play = $video_list[array_rand($video_list)];
    // Now you can display the $video_to_play variable, which
    // contains a random video from your database.
}

This code first checks if the query was successful and then checks if any rows were returned by the query. If the query was successful and returned at least one row, it will create an array of all the videos in the row and then choose a random video from that array.

I hope this helps! Let me know if you have any other questions.

Thank you so much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.