Ver Mensaje Individual
  #2 (permalink)  
Antiguo 01/05/2016, 20:31
pcid
Usuario no validado
 
Fecha de Ingreso: abril-2008
Mensajes: 50
Antigüedad: 16 años
Puntos: 16
Respuesta: tabla dinamica siempre me devuelve el mismo registro en vez de todos?

Código MySQL:
Ver original
  1. create table post (id_post int auto_increment, titulo varchar(100), fecha timestamp, leido tinyint default 0, primary key (id_post));
  2. insert into post (titulo, fecha, leido)
  3. values ('Veinte mil leguas de viaje submarino', now(), 0),
  4.         ('Un capitán de 15 años', now(), 1),
  5.         ('Ilusiones', now(), 1),
  6.         ('Raíces', now(), 1),
  7.         ('Los pilares de la tierra', now(), 1),
  8.         ('Divergente', now(), 0);

Código PHP:
<?php
    $mysqlHost 
'localhost';
    
$mysqlDB 'dbName';
    
$mysqlUser 'userName';
    
$mysqlPassword 'userPassword';

    
$cn = new PDO('mysql:host='.$mysqlHost.';dbname='.$mysqlDB$mysqlUser$mysqlPassword);
    
$qry 'select id_post, titulo, fecha, leido from post order by id_post desc';
    
$result $cn->prepare($qry);
    
$result->execute();
    
$xc  '';
    
$xc .= '<table border="1" cellspacing="3" cellpadding="3" style="border-collapse:collapse;">';
        
$xc .= '<thead><tr>';
            for (
$i 0$i $result->columnCount(); $i++) 
            {
                
$aux $result->getColumnMeta($i);
                
$xc .= '<td>'.$aux['name'].'</td>';                
            }
        
$xc .= '</tr></thead>';
        
$xc .= '<tbody>';
            while(
$row $result->fetch(PDO::FETCH_NUM))
            {
                
$xc .= '<tr>';
                for (
$i 0$i $result->columnCount(); $i++) $xc .= '<td>'.trim($row[$i]).'</td>';
                
$xc .= '</tr>';
            }
        
$xc .= '</tbody>';
    
$xc .= '</table>';
    echo 
$xc;
?>
Saludos,