I can not list a table in php

2

I can not list a simple table in php ... I do not see what's wrong with the code, when I run this php code it just shows me a blank page ... What am I 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>

    
asked by anonymous 07.05.2018 / 20:30

1 answer

2

There is a lot of wrong thing in this code, my friend, one of them is there in your <title> :

<title>Listar Alunos</alunos>

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

<title>Listar Alunos</title>

And another, mysql is obsolete, change to mysqli, it is easier to adapt to it than to the PDO.

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

$resposta = mysqli_query($conn, $query)

And another, do not use an array here:

$row = mysql_fetch_array($resposta)

Use assoc :

$row = mysqli_fetch_assoc($resposta)

I do not think it changes much more than adding a i to your mysql .

Following is a connection.php file:

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

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

?>

On your index.php page, right after you open the PHP tag,

include_once("conexao.php")

And then you can put $conn in mysqli_query .

    
07.05.2018 / 20:44