How to create a page that displays database entries?

Asked

Viewed 69 times

2

I have a system with a registration page, I would like another page that shows all the records that have been made, cute as a matrix.

EXAMPLE:

ID NAME E-MAIL

01 Mariana [email protected]
02 Bayonetta [email protected]

I know it goes against the policy of stackoverflow but I still have no code ready, I’ve been researching on foruns how to do this but everyone seems very confused.

I looked for the stackoverflow in the hope that someone could forward me the answer in a way explained.

  • 1

    Do you have any knowledge in ajax ?

  • I have no knowledge of ajax, but if I have no other way, I can research.

  • 1

    There is a very good plugin, which makes all pagination, data filtering and etc. but you need to know ajax to use it

  • 1

    I think what you need is similar to this: https://answall.com/a/54631/69359

2 answers

3


A simple way to display the database’s registered data using php would be like this:

   <?php
    // definições de host, database, usuário e senha
    $host = "nome_do_host"; 
    $db   = "nome_do_banco"; 
    $user = "nome_do_usuario"; 
    $pass = "senha_do_banco";
    // conecta ao banco de dados
    $con = mysql_pconnect($host, $user, $pass) or trigger_error(mysql_error(),E_USER_ERROR); 
    // seleciona a base de dados em que vamos trabalhar
    mysql_select_db($db, $con);
    // cria a instrução SQL que vai selecionar os dados
    $query = sprintf("SELECT * FROM sua_tabela");
    // executa a query
    $dados = mysql_query($query, $con) or die(mysql_error());
    // transforma os dados em um array
    $linha = mysql_fetch_assoc($dados);
    // calcula quantos dados retornaram
    $total = mysql_num_rows($dados);
    ?>

    <html>
        <head>
        <title>Exemplo</title>
    </head>
    <body>
    <?php
        // se o número de resultados for maior que zero, mostra os dados
        if($total > 0) {
            // inicia o loop que vai mostrar todos os dados
            do {
    ?>
                <p><?=$linha['id']?> / <?=$linha['nome']?></p> / <?=$linha['email']?></p>
    <?php
            // finaliza o loop que vai mostrar os dados
            }while($linha = mysql_fetch_assoc($dados));
        // fim do if 
        }
    ?>
    </body>
    </html>
    <?php
    // tira o resultado da busca da memória
    mysql_free_result($dados);
    ?>

1

This code was taken from a forum and worked for me:

<?php

//Conexão e consulta ao Mysql
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('db_formacao') or die(mysql_error());
$qry = mysql_query("select * from formacoes");

//Pegando os nomes dos campos
$num_fields = mysql_num_fields($qry);//Obtém o número de campos do resultado

for($i = 0;$i<$num_fields; $i++){//Pega o nome dos campos
    $fields[] = mysql_field_name($qry,$i);
}

//Montando o cabeçalho da tabela
$table = '<table border="1"><tr>';

for($i = 0;$i < $num_fields; $i++){
    $table .= '<th>'.$fields[$i].'</th>';
}

//Montando o corpo da tabela
$table .= '<tbody>';
while($r = mysql_fetch_array($qry)){
    $table .= '<tr>';
    for($i = 0;$i < $num_fields; $i++){
        $table .= '<td>'.$r[$fields[$i]].'</td>';
    }
    $table .= '</tr>';
}

//Finalizando a tabela
$table .= '</tbody></table>';

//Imprimindo a tabela
echo $table;

?>

Browser other questions tagged

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