What is the error in this while PHP?

Asked

Viewed 207 times

-2

The code below selects all the results whose thumbnail of the video has not yet been created and if it has not been, it creates a new one, but I am getting an error that I cannot find the solution:

    $video = BD::conn()->prepare("SELECT * FROM episodios WHERE thumb = '0' OR thumb = ''");
    $video->execute();
    $c = $video->rowCount();
    if($c >= 1){
        while($dados = $video->fetch()){
            $id = $dados["id"];
            $serie = $dados["id_serie"];
            $temporada = $dados["temporada"];
            $episodio = $dados["episodio"];

            if (!is_dir("D:/thumbs/".$serie)) {
                mkdir("D:/thumbs/".$serie, 0777);
            }
            if (!is_dir("D:/thumbs/".$serie."/".$temporada)) {
                mkdir("D:/thumbs/".$serie."/".$temporada, 0777);
            }

            /*o erro está sendo gerado aqui*/   
            if(is_dir("D:/videos/".$anime."/original/")) {
                $video = "D:/videos/".$serie."/original/".$temporada."/".$episodio.".mp4";
            }else{
                $video = "D:/videos/".$anime."/dublado/".$temporada."/".$episodio.".mp4";
            }
            /*fim da parte com erro*/
        }
  }

When I comment on the block that is supposedly generating the error, while normally goes through, but when I add the same I get this error:

Fatal error: Call to a Member Function fetch() on string in

  • its variable $video is a string and not an object.

1 answer

4


Turns out you’re calling the function FETCH in the object $video but at the end of the code you’re on writing the variable that had a OBJECT with a STRING, there the error.

Change the ending to:

if(is_dir("D:/videos/".$anime."/original/")) {
            $video1 = "D:/videos/".$serie."/original/".$temporada."/".$episodio.".mp4";
        }else{
            $video1 = "D:/videos/".$anime."/dublado/".$temporada."/".$episodio.".mp4";
        }

Remember that $video1 will only have the last Loop value, if you want to store all values you need to use an Array, for example $videos[$id]

  • I can’t just "reset" the video variable and recreate it inside the loop?

  • I don’t think it makes sense, because you are inside the $video object and processing the while, subscribing it would be difficult to pick up where you left off, in addition to the gambiarra.

  • If you do this the Loop would be infinite always running the first iteration. Use the Array $videos[$id] that should solve you. Out of While writes the following: print_r($videos); that it will display to you the array that was created.

  • I would not like to store the values in an array to use outside of while, I would like to run everything within the same loop, including the creation of Thumb, it is not possible to do this inside while?

  • Create what? An HTML? Just use echo "D:/videos/".$serie."/original/".$temporada."/".$episodio.".mp4";

  • I just need to pass to the $video variable the path where the video is to be used by ffmpeg

  • Then save inside a variable called $videoFfmpeg and use this variable in the function of ffmpeg that has to be called every time inside the Loop.

  • That’s what I’m trying to do in the $video variable and I’m having this mistake

  • Because of the variable name, it cannot be $video. Change to $video1;

  • Why does this error occur using the variable with that name? It’s some kind of PHP-reserved name?

  • 1

    No, you are using the $video variable twice even. Once as Object SQL extracting FETCH and at the end of Loop you use the same $video variable to receive a STRING, only STRING does not have the FETCH function and hence the Error.

  • Aah hadn’t noticed this duplicate, because before I was doing this process in two different pages, now I’m putting both together and I didn’t touch it. Thanks man

  • Let’s have a hug!

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.