Problem with while loop in PDO

Asked

Viewed 1,028 times

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
  }
   ?>
  • 1

    You forgot the two points (:) in the :active parameter array there, and you can use fetchAll to return all the data.

  • If you run the query directly by the database it returns more than one line? fetch returns only one line, fetchAll all.

2 answers

0


You forgot to add the two points in the parameter array, in the case of the parameter activo (:activo) and to return more than one data, 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);
}

0

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

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

Browser other questions tagged

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