0
Good people I’m here with a problem in the PDO
I want to do a while but did a query
and in the end only I returned a value of that while
I don’t know what’s going on
Code
$result = $conexao->prepare("SELECT * FROM colecoes WHERE menu = :menu AND activo = :activo ORDER BY pos ASC ");
$result->execute(array(':menu' => 'Comer', 'activo' => '1'));
while($row = $result->fetch(PDO::FETCH_OBJ)){
Example
<?php
$result = $conexao->prepare("SELECT * FROM colecoes WHERE menu = :menu AND activo = :activo ORDER BY pos ASC");
$result->bindValue(':menu', 'Comer', PDO::PARAM_STR);
$result->bindValue(':activo', 1, PDO::PARAM_INT);
$result->execute();
while($row = $result->fetchAll(PDO::FETCH_OBJ)) {
$result_anex = $conexao->prepare("SELECT * FROM colecoes_anexos WHERE id_mae = :row_id AND seccao = :seccao");
$result_anex->bindValue(':row_id', $row->id, PDO::PARAM_INT);
$result_anex->bindValue(':seccao', 'thumbnail', PDO::PARAM_STR);
$result_anex->execute();
$row_anex = $result_anex->fetch(PDO::FETCH_OBJ);
$result_count = $conexao->prepare("SELECT COUNT(*) AS id FROM categorias_estabelecimentos WHERE categoria_slug = :categoria_slug ") or
die(mysql_error());
$result_count->execute(array(':categoria_slug' => $row->slug));
$bar = $result_count->fetch(PDO::FETCH_OBJ);
?>
<!--item-->
<div class="entry one-fourth wow fadeInLeft">
<figure>
<a href="estabelecimentos/<?php echo $row->slug; ?>" style="cursor:pointer;"><img style="border-radius:10px;" src="gtm/anexos/colecoes/<?php echo $row_anex->id_anexo; ?>.<?php echo $row_anex->tipo ?>" alt="<?php echo utf8_encode($row_anex->titulo); ?>&h=203&w=270&a=c" /></a>
<a href="estabelecimentos/<?php echo $row->slug; ?>"><div style="position:absolute; margin-left:0px; color:white; font-weight: bold; background: rgba(54, 25, 25, .5); padding:2px 5px 0px 20px; margin-top:-140px;"><?php echo $bar['id']; ?> ESTABELECIMENTOS</div></a>
<a href="estabelecimentos/<?php echo $row->slug; ?>"><div style="position:absolute; margin-left:0px; color:white; font-weight: bold; background: rgba(54, 25, 25, .5); padding:2px 5px 0px 20px; margin-top:-100px;"><?php echo utf8_encode($row->titulo); ?></div></a>
<!--<figcaption><a href="recipe.html"><i class="ico i-view"></i> <span>View recipe</span></a></figcaption>-->
</figure>
</div>
<!--item-->
<?php
}
?>
You forgot the two points (:) in the :active parameter array there, and you can use fetchAll to return all the data.
– Rafael Withoeft
If you run the query directly by the database it returns more than one line?
fetch
returns only one line,fetchAll
all.– rray