I cannot list a table in php

Asked

Viewed 54 times

2

I cannot list a simple table in php... I don’t see what’s wrong with the code, when I run this php code just shows me a blank page... What I’m doing wrong?

<html>
<head>
    <title>Listar Alunos</alunos>
</head>
<body>
    <?php

     //Conecta ao servidor e escolhe a base de dados
     mysql_connect("localhost", "root", "");
     mysql_select_db("login");

     $query = "SELECT * FROM alunos";


     if ($resposta = mysql_query($query)){
         echo
         '
         <table align = "left" cellspacing = "5" cellpadding = "8">
         <tr><th align = "left"><b>Primeiro Nome</b></th>
         <th align = "left"><b>Último Nome</b></th>
         <th align = "left"><b>Email</b></th>
         <th align = "left"><b>Rua</b></th>
         <th align = "left"><b>Cidade</b></th>
         <th align = "left"><b>Telemóvel</b></th>
         <th align = "left"><b>Data de Nascimento</b></th>
         <th align = "left"><b>Sexo</b></th><tr>
         ';

         while($row = mysql_fetch_array($resposta)){
             echo '<tr><td align = "left">' .
             $row['primeiro_nome'] . '</td><td align = "left">' .
             $row['ultimo_nome'] . '</td><td align = "left">' .
             $row['email'] . '</td><td align = "left">' .
             $row['rua'] . '</td><td align = "left">' .
             $row['cidade'] . '</td><td align = "left">' .
             $row['telemovel'] . '</td><td align = "left">' .
             $row['data_aniversario'] . '</td><td align = "left">' .
             $row['sexo'] . '</td>' ;

             echo '</tr>';
         }
         echo '</table>';

         } else{
            echo "Não foi possível listar os alunos. ".mysql_error();
         }

    ?>
</body>

  • mysql is obsolete, use mysqli or Pdo

  • is for a school project... Since I can’t touch the machines there, unfortunately I have to use in php 5 version... Think the problem may be from there?

  • You are testing locally, or on the school server?

  • This file has that extension?

  • locally, with the shaman

  • is called listing.php

  • Share this listing code.php.

Show 2 more comments

1 answer

2


There’s a lot wrong with your code, my friend, one of them is in your code <title>:

<title>Listar Alunos</alunos>

Are you closing the tag <title> with </alunos>, then everything you do after that, will be in your title. You can put the mouse on top of your tab and see that all your syntax is there. Change to:

<title>Listar Alunos</title>

And another, mysql is obsolete, switch to mysqli, it is easier you adapt to it than to PDO.

I can help you migrate your code to mysqli (add an i) and you create a file php connection. and before your $resposta = mysql_query($query) you will put:

$resposta = mysqli_query($conn, $query)

And another, don’t use one array here:

$row = mysql_fetch_array($resposta)

Use a Assoc:

$row = mysqli_fetch_assoc($resposta)

Otherwise I think it doesn’t change much except to add one i in his mysql.

Follows a file php connection.:

<?php
  $servidor = "localhost";
  $usuario = "root";
  $senha = "123";
  $dbname = "teste";

  //Criar Conexão
  $conn = mysqli_connect($servidor, $usuario, $senha, $dbname);

?>

And on your page index php., right after you open the tag PHP, place:

include_once("conexao.php")

And then you get to put the $conn in the mysqli_query.

  • Thanks! Just had to change the title code and it all worked...

  • Sometimes things are really stupid that pass us xd

  • For nothing, friend, and yes, sometimes things go by so simple that we don’t give them much notice, we just focus on the hardest part of the code because it’s the first thing a programmer thinks about when something isn’t working in the code. That’s why I recommend a good code editor, because some indicate where to close such tag and thus makes it easy to avoid these silly mistakes kk

Browser other questions tagged

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