PHP PDO - MySQL query data does not appear in the table

0

What am I doing wrong in this script?

I make a query in the database to return all the data from the curso table and display their names in the table, but it is not working.

In addition this part of the code is being displayed 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><scriptsrc="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><!--Includeallcompiledplugins(below),orincludeindividualfilesasneeded--><scriptsrc="bootstrap/js/bootstrap.min.js"></script>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script><scriptsrc="js/bootstrap.min.js"></script>
      <script src="js/custom.js"></script>
  </body>

  </html>
    
asked by anonymous 22.04.2017 / 06:19

1 answer

1

You have several errors in your code, but I have tried to solve your error:

  

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

Structure of table curso (I created for testing 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>

Result:

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

You should compare my code to yours for teaching purposes.

    
22.04.2017 / 10:35