View image stored in blob with PDO?

Asked

Viewed 662 times

0

I stored an image using the field BLOB mysql (LONGBLOB), but I can’t show it on html:

//Instanciando minha classe com PDO e fazendo um 
//SELECT de todos os dados da tabela, inclusive a imagem
<?php
   $DAO = new DAO();
   $result = $DAO->select("SELECT * FROM TB_ADVOGADOS ORDER BY DT_CREATED DESC", array());
?>

//O foreach da tabela com os dados e a imagem
<?php foreach ($result as $row) { 
   $createDate = new DateTime($row['DT_CREATED']); ?>
   <tr>
     <td><input type="checkbox" name="ckhbox" /></td>
     <td><?php echo $row['TXT_NAME']; ?></td>
     <td><?php echo $row['NM_AGE']; ?></td>
     <td><?php echo $row['TXT_DESCRIPTION'] ?></td>
     <td><?php echo $createDate->format('d-m-Y') ?></td>
     <td><?php echo $row['PIC_IMAGE'] ?></td>
   </tr>
<?php } ?>

I believe I need to format mine $row['PIC_IMAGE'] for something that recognizes this string BLOB and found some examples with mysql_fetch_object, but none with PDO that I can use. Just to help understand, I’ll leave below the form I used to store the image in the bank:

<?php 
   $imageTmp = $_FILES['image']['tmp_name'];
   $imageSize = $_FILES['image']['size'];
   $imageType = $_FILES['image']['type'];
   $imageName = $_FILES['image']['name'];

    if($imageTmp != "none"){
       $fileManager = fopen($imageTmp, "rb");
       $conteudo = fread($fileManager, $imageSize);
       $conteudo = addslashes($conteudo);
       fclose($fileManager);
    }
    $DAO = new DAO();
    $result = $DAO->query("INSERT INTO TB_ADVOGADOS (ID_USER, TXT_NAME, NM_AGE, 
    TXT_DESCRIPTION, PIC_IMAGE) VALUES (:ID, :NAME, :AGE, :DESCRIPTION, :IMAGE)", 
    array(":ID"=> $idUser, ":NAME"=> $name, ":AGE"=> $age, 
    ":DESCRIPTION"=> $description, ":IMAGE"=> $conteudo));

?>

Any hint on how I can display them?

EDIT:

Below is the content of my DAO class:

<?php 

class DAO extends PDO {

private $conn;

public function __construct(){
    $this->conn = new PDO("mysql:host=localhost;dbname=meu_banco","usuario","senha");
}

private function setParams($statement, $parameters = array()){
    foreach($parameters as $key => $value){ 
        $this->setParam($statement, $key, $value);
    }   
}

private function setParam($statement, $key, $value){
    $statement->bindParam($key, $value);
}

public function query($rawQuery, $params = array()){
    $stmt = $this->conn->prepare($rawQuery);
    $this->setParams($stmt, $params);
    $stmt->execute();
    return $stmt;
}

public function select($rawQuery, $params = array()){
    $stmt = $this->query($rawQuery, $params);
    return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

}

?>
  • The content of $row['PIC_IMAGE'] should not be inside a <img> ?

  • @Guilhermenascimento has two ways that I know how to do this, if I asked a question and answers in both ways would be cool?

  • @Virgilionovic could not answer the question of dup https://answall.com/q/48325/3635? If you do not confirm that I am reopening this one ("golden hammer" you can immediately reopen questions).

  • So @Guilhermenascimento the question seems to encompass a guy, now I was in doubt, because the biggest problem is that this question is open ... I do not know honestly kkkk why I asked ...

  • @Guilhermenascimento The question marked as duplicate did not help in my particular case :/ The programmer of the question in question uses mysqli, and does not fit my case. Displaying the image using '<img>' works for me as recommended by rray, but the problem seems to be in the way the image comes from the database. in var_dump($Row['PIC_IMAGE']) an empty vector is returned. We managed to reopen the question?

  • mysql_fetch_object is the API "ancient" PHP and has nothing to do with PDO, namely both PDO Apis and mysql_fetch_object "do not communicate", So this is the problem, if you’re using PDO, just use its functions and don’t mix with other Apis because it won’t work at all. Apparently the same problem was with this question https://answall.com/q/233485/3635 ... Including the old API functions, which start with the prefix mysql_ are obsolete and have been removed in PHP7, so do not use the old API as it has been discontinued for a long time, use PDO and/or mysqli.

  • I didn’t quite understand the problem... that addslashes() shouldn’t be in the code. mysql_fetch_object would do what?

  • @rray if this DAO class uses prepared then addslashes may not even be necessary and may be something else that is causing failure.

  • @Guilhermenascimento will not be recording 'chewed'/ incorrect the blob? so the problem only at the time of displaying?

  • @rray is possible, yet I think the main mistake is that it is mixing PDO with the old api, as I mentioned in the previous comment, it can be a series of errors :/

  • Srs, mysql_fetch_object really has no connection with my problem. In fact, I’ve seen several examples of users using mysqli_fetch_array to handle their return from the database and be able to display the image. I did not use it at any time. It was just a reasoning my very poorly explained, by the way hehe. In short, I think I need to treat my return from the database in some way so that html recognizes that string in an image, you know?

  • Post the content of the DAO class, probably the problem is in it. Besides that as I said, if using Prepared the use of addslashes may be truncating the data.

  • @Csorgo first, remove the addslash part, and second change all this: $fileManager = fopen($imageTmp, "rb");&#xA; $conteudo = fread($fileManager, $imageSize);&#xA; $conteudo = addslashes($conteudo);&#xA; fclose($fileManager); for this alone $conteudo = file_get_contents($imageTmp) or die('Erro ao pegar o upload'); and then try to upload a new image and try to display it with var_dump($row['PIC_IMAGE']);

  • @Guilhermenascimento made the requested changes, and now I have a gigantic string like this ( Ś9l9{1t j+L) on my page. It seems that html is not recognizing what comes from the bank.

  • @Csorgo this is the new file you sent after modifying to file_get_contents?

  • @Guilhermenascimento said so. The two old records were left with the frame of the img tag, but without loading its internal content.

  • @Csorgo ok, put the content of the new record in a Pastebin.com and send me.

Show 12 more comments
No answers

Browser other questions tagged

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