Problem with while loop in PDO

0

I'm not sure what to do, but I do not know how to do it.

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
  }
   ?>
    
asked by anonymous 06.03.2015 / 23:05

2 answers

0

You forgot to add the colon in the parameter array , in case the parameter activo (:activo) is to return more than one die, use fetchAll.

Follow the final code:

try
{    
    $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();
    $rows = $result->fetchAll(PDO::FETCH_ASSOC);
    foreach ($rows as $row) {
        //Continuação
    }
}
catch (PDOException $e)
{
    var_dump($e);
}
    
06.03.2015 / 23:15
0

while(false != ($row = $fetch->fetchAll(arg))){}

Try this and in case of doubts I recommend that you visit the php.net site

    
07.03.2015 / 03:17