Listing data from a table with PHP

1

Hello, I'm trying to make a listing with data in a table with the code below, but I'm not getting it. I've done it in several ways, I've seen tutorials on the internet and everything, but it does not work. On the server both local and external nothing appears, everything is white. Could someone tell me if there is an error in this code?

    <?php
    require_once("Conn.php");
    $conexao = conectar();

    $query = "SELECT * FROM tab_final";
        try{
            $selecionar = $conexao->prepare($query);
            $selecionar->execute();
            $select = $selecionar->fetchAll(PDO::FETCH_OBJ);
          foreach($select as $listar){
            echo "Times: ".$listar->Nome."<br />";
          }
        }catch(PDOException $e){
            echo $e->getMessage();

     }
    ?>

I used up the FETCH_ASSOC and changed the echo to conform to it and nothing. I do not know what else to do. If anyone can test and succeed, help me, please! Thank you!

    
asked by anonymous 11.02.2017 / 00:33

1 answer

1

It is very simple to do a simple listing using PHP + MySQL. If you are a beginner, I advise you to do everything first on a page, then you can organize according to the pattern you want. Here is a simple example:

Connection example:

<?php
// Instancia o objeto PDO
$pdo = new PDO('mysql:host=localhost;dbname=db', 'root', '');
// define para que o PDO lance exceções caso ocorra erros
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>

The simplest way to execute a query would be to use query as the example:

<?php
// executa a instrução SQL
$consulta = $pdo->query("SELECT fullname, email FROM user;");
?>

To get the data a while can be used, thus traversing each row returned from the database:

<?php
while ($linha = $consulta->fetch(PDO::FETCH_ASSOC)) {
    // aqui eu mostro os valores de minha consulta
    echo "Fullname: {$linha['fullname']} - email: {$linha['E-mail']}<br />";
}
?>

I put the complete code in GitHub as PDO.php to stay logged in.

You can see more details on PHP Data Objects (en) and PDO: Query ( en) .

    
11.02.2017 / 01:01