List BD data using echo (PDO)

Asked

Viewed 595 times

0

Hello, I have this code:

<<?php 
include("conexao.php");
$pdo = conectar();

$buscarusuario = $pdo->prepare("SELECT * FROM tab_clientes WHERE ID=:id");
$buscarusuario->bindValue(":id",2,PDO::PARAM_INT);
$buscarusuario->execute();

$linha = $buscarusuario->fetchAll(PDO::FETCH_OBJ);
foreach ($linha as $listar) {
echo "E-mail: ".$listar->email."</br>";
var_dump($listar);
}

My problem is that the echo is not displayed on the screen, tested with the var_dump and it shows the BD data. Using FETCH_ASSOC normal, but since I’m learning PDO, I wanted to do this using the FETCH_OBJ, however, as I said, echo is not displayed on the screen. Does anyone have an idea of why this occurs or what other way to display? I thank you in advance!

2 answers

2


Try this

 <?php 
    include("conexao.php");
    $pdo = conectar();

    $buscarusuario = $pdo->prepare("SELECT * FROM tab_clientes WHERE ID=:id");
    $buscarusuario->bindValue(":id",2,PDO::PARAM_INT);
    $buscarusuario->execute();

    while($listar = $buscarusuario->fetch(PDO::FETCH_OBJ)){
    echo "E-mail: ".$listar->email."</br>";
    var_dump($listar);
    }
  • So it worked, thank you very much, but just one more thing, because I’m treating the variable as an object, because I’m using FETCH_OBJ, the foreach cannot be applied?

0

little brother you have to treat the result if I am not mistaken, play the variable that receives the result in an array and then do the foreach in the array

Browser other questions tagged

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