2

I have a website where people can upload their videos and if a user uploads a video it makes a new folder called videos in their folder e.g. user/Someone/videos/videoname and in the videos folder there are all the videos that the user uploaded. Then it inserts the video into the database. That works fine, but when I try to echo these videos in an HTML video tag I have problems. Since a user can upload more videos I created an array called $url. Then I select those videos that belong to that user ($sql = "SELECT * FROM videos WHERE user=?";). Then I push every video item to that $url array and I echo the videos with a foreach loop. And here is something wrong. There're duplicates, so one video will be echoed more times and there're missing urls.

Code:

$echo_videos = "";
$items = "";
$url = array();
$sql = "SELECT * FROM videos WHERE user=?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s",$u);
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()){
    $name = $row["video_file"];
    $items .= "user/$u/videos/$name";
    array_push($url, $items);
    foreach ($url as $key) {
        $echo_videos .= '<video width="400" controls><source src="'.$key.'"></video>';
    }
}

PS: Maybe the $items variable also should be an array but then I won't be able to push an array to another.

1 Answer 1

1

why not you simply echo the filename

while($row = $result->fetch_assoc()){
   echo '<video width="400" controls><source src="user/'.$u.'/videos/'. $row["video_file"].'">
         </video>';
}
Sign up to request clarification or add additional context in comments.

5 Comments

Almost good solution, but I'd correct your answer to this: while($row = $result->fetch_assoc()){ echo '<video width="400" controls><source src="user/'.$u.'/videos/'. $row["video_file"].'"> </video>'; }
And I don't know why but the first video does not appear.
When I comment out that line the $row["video_file"] will be equal to nothing.
i don't understand you last comment .. don't tell you of comment the line with the $row["video_file"]
You wrote I should comment out line $result = $stmt->get_result(); but you've already changed that comment and deleted that part... but when I comment out the code will not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.