PHP with PDO returning BLOB records with foreach

Asked

Viewed 195 times

3

Good afternoon guys, I’m new here (first question), but Stack has already helped me in several situations.

That’s the problem:

I am developing an application and on a given page there is a user profile page. On this page, a div that displays photos of users belonging to the same industry as the logged in user.

Query:

$query3 = "SELECT USR.EMAIL, USR.IMG_PERFIL, IMG.IMAGEM FROM IN_USUARIOS USR, IN_IMAGENS IMG WHERE USR.IMG_PERFIL = IMG.ID AND USR.SETOR =:setor AND USR.ID !=:id";

Running:

$stmt3 = $conn->prepare($query3);
$stmt3->bindValue(':setor',$result1['SETOR']);
$stmt3->bindValue(':id',$result1['ID']);
$stmt3->execute();
$result3=$stmt3->fetchAll(PDO::FETCH_ASSOC);

*$result1 = Returns all user data

Listing:

 <ul class="friends">
  <?php
   foreach($result3 as $key => $value ) 
    { 
      echo
        '<li>
          <div class="profile-pic">
            <img width="35" height="35" title="'. $value['EMAIL'] .'" src="data:image/jpeg;base64,'.base64_encode(stream_get_contents($value['IMAGEM'])).'">
          </div>
         </li>';
    }
  ?

The code presents the images, but always the same, in case the image of the last colleague in the data array: (should come the image corresponding to each of the logged user’s colleagues.

Print

  • notice that a title will be in the image according to the email of the colleague, this is working, bringing the emails of each of the colleagues.

var_dump($result3):

    array(4) {
  [0]=>
  array(3) {
    ["EMAIL"]=>
    string(30) "[email protected]"
    ["IMG_PERFIL"]=>
    string(1) "4"
    ["IMAGEM"]=>
    resource(3) of type (stream)
  }
  [1]=>
  array(3) {
    ["EMAIL"]=>
    string(29) "[email protected]"
    ["IMG_PERFIL"]=>
    string(1) "6"
    ["IMAGEM"]=>
    resource(4) of type (stream)
  }
  [2]=>
  array(3) {
    ["EMAIL"]=>
    string(26) "[email protected]"
    ["IMG_PERFIL"]=>
    string(1) "7"
    ["IMAGEM"]=>
    resource(5) of type (stream)
  }
  [3]=>
  array(3) {
    ["EMAIL"]=>
    string(26) "[email protected]"
    ["IMG_PERFIL"]=>
    string(1) "5"
    ["IMAGEM"]=>
    resource(6) of type (stream)
  }

}

  • Viewing code from page:

inserir a descrição da imagem aqui

I must be forgetting some detail in this my foreach. I count on your help.

Thank you.

  • gives a var_dump($result3); before the foreach and see if the data is all there...

  • @Localhost I added var_dump($result3); the question, see.

  • try to put as I put in the answer there, if not comment. I will analyze better here

  • @Localhost continues to show the same result. I right-clicked "view source code", went to the part of the code that should bring the images and noticed that the image hash in Base64 is the same... (I added the image the question)

  • Two questions. First, what is the reason to use different instead of equal here? USR.ID !=:id and second, USR.IMG_PERFIL = IMG.ID is IMG_PERFIL even the profile ID field of the images?

  • @Bacco Answer 1: Because I want to bring everyone from the same sector (USR.SECTOR=:sector) minus the logged in user (USR.ID !=:id). Answer 2: IMG_PERFIL is a field in the user table that has an FK for the image table, where the PK is the ID field.

  • A strange detail, but that can help us is that depending on the user who is logged in, the image that is repeated changes. Ex: Logged as GABRIEL the image that repeats in colleagues is the user JOÃO, logged as FÁBIO the image that repeats is GABRIEL, etc...

  • this PK is the user ID field?

  • @Bacco PK=Primary Key, is the key of the table, that is, it cannot be repeated. It is a unique id per record. in the image table, for example, is the ID field which is a number(6) and in the users table the PK is EMAIL which is a varchar2(35). I am using Oracle database.

  • actually now that I’ve cleared the query here to better understand, it got kind of bad to read in the question.

  • The question is kind of strange, but you have already made a simple user and photo listing, just to see if the pairs are correct in DB?

Show 6 more comments
No answers

Browser other questions tagged

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