Switch Case PDO does not display

Asked

Viewed 112 times

0

Changed further inside the title yet, the case works but does not display the result.

<?php

$sql = "SELECT tipo FROM publicidade WHERE posicao='Lateral' ORDER BY RAND() LIMIT 1";
$stmt = DB::prepare($sql);
$stmt->execute();
$exibe = $stmt->fetchAll();
foreach ($exibe as $u) {
$tipo = $u->tipo;  // <-- corrigido baseado nos comentarios
switch ($tipo) {
case 'imagem':
echo "<img src='img/publicidade/$u->idpublicidade/$u->imagem' heigth='150' width='100%'>";
break;
case 'flash':
echo "<embed src='img/publicidade/$u->idpublicidade/$u->flash' width='720' height='90'></embed>";
break;
case 'codigo':
echo "$u->codigo";
break;
}}

altered

1 answer

2

FetchAll does what it says: it searches all the results for a query. Since the search mounts results, you will get an indexed matrix.

You should change all queries for prepared instructions and replace the following:

  • $stmt->fetchAll(); for $stmt->fetchAll(PDO::FETCH_ASSOC);
  • or else $tipo = $u['tipo']; for $tipo = $u->tipo;

Browser other questions tagged

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