PDO Selection in mysql database parameter BLOB

Asked

Viewed 370 times

2

I am having trouble trying to rescue one or a list of images that I have already inserted in mysql 5 with PDO. I have a dao class that has the architecture of my querys to work with the database.

class ImagemDao {

public function inserirImagem(ImagemEntity $imagem) {
    $conexao = ...
    $stmt = $conexao->getStance()->prepare("INSERT INTO imagem(imagem,id_pagina) VALUES(:img, :idPagina)");
    $stmt->bindValue(":img", $imagem->getImagem());
    $stmt->bindValue(":idPagina", $imagem->getIdPagina());
    $stmt->execute();
    
}
public function selecionaTodasImagens() {
    $conexao = ...
    $consulta = $conexao->getStance()->query("SELECT id_imagem, imagem, id_pagina FROM imagem;");
    while ($linha = $consulta->fetch(PDO::FETCH_OBJ)){
        
        echo $linha->id_imagem ."</br>" ;
        echo $linha->imagem ."</br>";
        echo $linha->id_pagina ."</br>";
    
    }
}

my bean is as follows.

class ImagemEntity {
private $id;
private $imagem;
private $idPagina;
function __construct($id = "", $imagem = "", $idPagina = "") {
    $this->id = $id;
    $this->imagem = $imagem;
    $this->idPagina = $idPagina;
}
 //get and set

and I have a test file

<?php 
    $dao = new ImagemDao();
    $dao->selecionaTodasImagens();

?>

what it returns to me are just the text of the image and its id but no view.

my bank entity is as follows. inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

in the browser is showing as follows.

  • Related: http://answall.com/questions/48325/slideshow-com-imagens-blob-do-mysql/

1 answer

2


Behold PDO - Large Objects (Lobs) .

Basically missed the constant PDO::PARAM_LOBwhen it’s time to make Binding...

$stmt->bindValue(":img", $imagem->getImagem(), PDO::PARAM_LOB);

Of course I’m assuming $imagem->getImagem() was previously initialized with a file or something like that (if you just pass the image name nothing will happen).

Browser other questions tagged

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