Problem with select php mysql [closed]

-2

I'm having a problem I've never seen in php, I'll try to set an example to make it easier to understand.

I have a arquivos table in this table I have 2 columns being id do arquivo and id do usuário (to whom this file belongs).

The user of id 8 is connected and in the system will list all files that have idUsuario 8 that are 4 files related to even just that my method of listing files always omitted a sample file are 4 files registered with idUsuario 8 it does not list me 4 files it is listing me 3 ie a file it

So I understand interpreting the output it is taking the amount of data that a given user has and always omitting the file of smaller id of the table

method listArchives

public function listarArquivo(){
    try{
        $id_usuario = $this->id;

        $query = new DbFunctions();
        $query->selectFiles("SELECT * FROM arquivos WHERE idUsuario = '$id_usuario'");

        //var_dump($id_usuario);
        foreach($query->getResult() as $chave => $dado){
            echo "<tr>
            <td>".$dado->id_arquivo."</td>
            <td>".$dado->nome."</td>
            <td>".$dado->tamanho."</td>
            <td>".$dado->data."</td>
            <td>
                <a href='visualizar_arquivo.php?acao=visualizar&id_arquivo=".$dado->id_arquivo."'target='_blank'><i class='fa fa-external-link fa-2x' aria-hidden='true'></i></a>
                <a href='visualizar_arquivo.php?acao=download&id_arquivo=".$dado->id_arquivo."'target='_blank'><i class='fa fa-cloud-download fa-2x' aria-hidden='true'></i></a>
                <a href='?link1=".$dado->id_arquivo."' name='link1' ><i class='fa fa-trash fa-2x' aria-hidden='true'></i></a>
            </td>";
        }
        //$query->getResult();
        echo "<pre>";
        var_dump($query->getResult());
        echo "</pre>";

    }catch(PDOException $e){
        echo $e->getMessage();
    }
}

selectFiles method:

public function selectFiles($sql){
    $query = $this->conecta()->query($sql);
    foreach($query as $row){
        $this->setResult($query->fetchAll(\PDO::FETCH_OBJ));
        //$this->setResult($row);
        return true;
    }
}

I do not know if this problem could be because I'm getting the result of the query and putting it on an object, would that object have a storage size?

It may be a bit confusing for me, but I do not understand the reason for this problem if someone can give me some help, thank you.

    
asked by anonymous 05.06.2018 / 23:58

3 answers

1

Probably the first line of the result is being omitted because of foreach($query as $row) , which moves the cursor forward in the ResultSet ($ query), causing fetchAll to skip the first record because it has already been processed by% with%. One possible solution is to change foreach to foreach , something like:

public function selectFiles($sql){
    $query = $this->conecta()->query($sql);
    //verifica se foram retornados registros pela consulta
    if($query->rowCount()){
       $this->setResult($query->fetchAll(\PDO::FETCH_OBJ));
       return true;
    }
 }
    
06.06.2018 / 23:11
-1

The method list files does not have the tr closing.

  

Correct example

public function listarArquivo(){
    try{
        $id_usuario = $this->id;

        $query = new DbFunctions();
        $query->selectFiles("SELECT * FROM arquivos WHERE idUsuario = '$id_usuario'");

        //var_dump($id_usuario);
        foreach($query->getResult() as $chave => $dado){
            echo "<tr>
            <td>".$dado->id_arquivo."</td>
            <td>".$dado->nome."</td>
            <td>".$dado->tamanho."</td>
            <td>".$dado->data."</td>
            <td>
                <a href='visualizar_arquivo.php?acao=visualizar&id_arquivo=".$dado->id_arquivo."'target='_blank'><i class='fa fa-external-link fa-2x' aria-hidden='true'></i></a>
                <a href='visualizar_arquivo.php?acao=download&id_arquivo=".$dado->id_arquivo."'target='_blank'><i class='fa fa-cloud-download fa-2x' aria-hidden='true'></i></a>
                <a href='?link1=".$dado->id_arquivo."' name='link1' ><i class='fa fa-trash fa-2x' aria-hidden='true'></i></a>
            </td>
          </tr>"; // <------ </tr> que faltava 
        }
        //$query->getResult();
        echo "<pre>";
        var_dump($query->getResult());
        echo "</pre>";

    }catch(PDOException $e){
        echo $e->getMessage();
    }
}
    
06.06.2018 / 02:27
-1

implementation of selectFiles, getResult and setResult methods

selectFiles method

public function selectFiles($sql){
    $query = $this->conecta()->query($sql);
    foreach($query as $row){
       $this->setResult($query->fetchAll(\PDO::FETCH_OBJ));
       return true;
    }
 }

getResult method

public function getResult(){return $this->result;}

method setResult

private function setResult($r){$this->result = $r;}
    
06.06.2018 / 20:01