PHP PDO - Mysql query data does not appear in table

Asked

Viewed 526 times

0

What I’m doing wrong in this script?

I make a query in the database to return all table data curso and show their names in the table, but it’s not working.

Also this part of the code is being shown on the page:

(prepare("SELECT * FROM curso"); if($consulta->execute()){ ?>
fetch(PDO::FETCH_OBJ)){ ?> '.$dados->idCursoLi . ''; echo ''; echo ''; } }?>
'.$dados->nomeCursoLi . '

My code:

<?php


  require '../conexao/conexao_BD.php';
?>

  <!DOCTYPE html>
  <html>

  <head>
    <title>Painel</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Bootstrap -->
    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">

    <link href="bootstrap/css/bootstrap.css" rel="stylesheet">
    <!-- styles -->
    <link href="../css/styles-Painel.css" rel="stylesheet">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
    <![endif]-->
  </head>

  <body>
    <div class="header">
      <div class="container">
        <div class="row">
          <div class="col-md-5">
            <!-- Logo -->
            <div class="logo">
              <h1><a>Painel do Administrador</a></h1>
            </div>
          </div>

          <div class="col-md-2">
            <div class="navbar navbar-inverse" role="banner">
              <nav class="collapse navbar-collapse bs-navbar-collapse navbar-right" role="navigation">
                <ul class="nav navbar-nav">
                  <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">Minha Conta<b class="caret"></b></a>
                    <ul class="dropdown-menu animated fadeInUp">
                      <li><a href="logout.php">Sair</a></li>
                    </ul>
                  </li>
                </ul>
              </nav>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="page-content">
      <div class="row">
        <div class="col-md-2">
          <div class="sidebar content-box" style="display: block;">
            <ul class="nav">
              <!-- Main menu -->
              <li class="current"><a href="painel.html"><i class="glyphicon glyphicon-home"></i> Home</a></li>
              <li class="submenu">
                <a href="#">
                  <i class="glyphicon glyphicon-list"></i> Administrador
                  <span class="caret pull-right"></span>
                </a>
                <!-- Sub menu -->
                <ul>
                  <li><a href="cadastrarAdm.html">Cadastrar</a></li>
                  <li><a href="#">Editar</a></li>
                  <li><a href="#">Remover</a></li>
                </ul>
              </li>

              <li class="submenu">
                <a href="#">
                  <i class="glyphicon glyphicon-list"></i> Curso
                  <span class="caret pull-right"></span>
                </a>
                <!-- Sub menu -->
                <ul>
                  <li><a href="cadastrarCurso.html">Cadastrar</a></li>
                  <li><a href="#">Editar</a></li>
                  <li><a href="#">Remover</a></li>
                </ul>
              </li>
            </ul>
          </div>
        </div>

      </div>




      <div class="container">
        <table class="table-bordered">

          <h2>Cursos cadastrados</h2>

          <?php $consulta = $PDO->prepare("SELECT * FROM curso");

              if($consulta->execute()){ ?>


          <table class="table table-bordered">
            <thead>
              <tr>
                <th>Curso</th>
                <th>Excluir</th>

              </tr>
            </thead>
            <?php while($dados = $consulta->fetch(PDO::FETCH_OBJ)){ ?>

            <tr>
              <?php echo '<td>'.$dados->idCursoLi . '</td>';
                                  echo '<td>'.$dados->nomeCursoLi . '</td>'; 
                                  echo '<td><a href="deletarCurso.php?id='.$dados->idCursoLi.'"><img src="delete.png" alt="Excluir curso" height="42" width="42"></a></td>';

                                  } }?>

            </tr>


          </table>


      </div>



      <footer>
        <div class="container">

          <div class="copy text-center">
            Copyright(@) <a href="inove.html">Inove.</a> Desenvolvido por: <a target="blank_" href="http://tecnosystemej.com/site/">Tecno System</a>
          </div>

        </div>
      </footer>

      <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
      <script src="https://code.jquery.com/jquery.js"></script>
      <!-- Include all compiled plugins (below), or include individual files as needed -->
      <script src="bootstrap/js/bootstrap.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
      <script src="js/bootstrap.min.js"></script>
      <script src="js/custom.js"></script>
  </body>

  </html>

1 answer

1


There are several errors in your code, but I aimed to solve your error:

PHP PDO - data from the Mysql database does not appear in the table

Table structure curso (I created for test purposes):

CREATE TABLE IF NOT EXISTS `curso` (
    `idCursoLi` INT(10) NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `nomeCursoLi` VARCHAR(50) COLLATE `utf8_unicode_ci` NOT NULL DEFAULT ''
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

INSERT INTO `curso` (`nomeCursoLi`) VALUES ('foo'), ('bar'), ('fubá'), ('canjica'), ('angu');

Page code (Table only):

    <div class="container">
        <table>
            <tr>
                <td colspan="3">
                    <h2>Cursos cadastrados</h2>
                </td>
            </tr>
            <?php

                $conexaoMySQL = new PDO(
                                'mysql:host='.'meu_host'.
                                ';dbname='.'meu_banco',
                                'meu_usuario',
                                'minha_senha',
                                array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") );

                $stmt = $conexaoMySQL -> prepare("SELECT * FROM `curso`;");
                if( $stmt -> execute() )
                {
                    $res = $stmt -> fetchAll(PDO::FETCH_OBJ);

                    foreach($res as $curso)
                    { ?>

                        <tr>
                            <td><?= $curso -> idCursoLi; ?></td>
                            <td><?= $curso -> nomeCursoLi; ?></td>
                            <td><a href="deletarCurso.php?id=<?= $curso -> idCursoLi; ?>"><img src="delete.png" alt="Excluir curso" height="42" width="42"></a></td>
                        </tr>

                    <?php
                    }
                }
            ?>
        </table>
    </div>

Upshot:

inserir a descrição da imagem aqui

PS: Remember to reconfigure the connection PDO with the database and implement the error handling (Exceptions)!

You should compare my code with yours for educational purposes.

  • 1

    Hi, thanks! I managed to fix, had two <table> tags, deleted and went! But this way you did using Expression Language is a good option for those who do not want to merge HTML cm PHP like I did.

Browser other questions tagged

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