Php7 display data from mysql

Here is an example of connecting to the MYSQL database and listing data in PHP7 with Html.

You must first create a table called product

-- `product` Table

CREATE TABLE `product` (
  `id` int(11) NOT NULL,
  `product` varchar(100) COLLATE utf16_spanish2_ci NOT NULL,
  `price` int(11) NOT NULL,
  `stock` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf16 COLLATE=utf16_spanish2_ci;

And then insert data

-- Data for `product` Table

INSERT INTO `product` (`id`, `product`, `price`, `stock`) VALUES
(1, 'Fossil Men\'s Grant Quartz Stainless Steel ', 88, 3),
(2, 'Fossil Men\'s ME3140 Grant Sport Automatic Luggage Leather Watch', 145, 8),
(3, 'Fossil Mens Modern Machine Sport - ME3135', 255, 12),
(4, 'Fossil Men\'s Stainless Steel Mechanical-Hand-Wind ', 124, 8);

And then create a php file.

<div class="container">
      <div style="height:50px"></div>
      <h1>< tutofox /> <small>Oh my code!</small></h1>
      <p class="lead">
      <h3>PHP7 Listar</h3>
      <p>Aqui esta un ejemplo de listar datos desde la base de datos mysql en PHP7</p>

      <input class="form-control col-md-3 light-table-filter" data-table="order-table" type="text" placeholder="Search..">


        $enlace = mysqli_connect("localhost", "newuser", "password", "tutofox");

        /* comprobar la conexión */
        if (mysqli_connect_errno())
            printf("Falló la conexión: %s\n", mysqli_connect_error());

        $consulta = "SELECT * FROM product ORDER by id DESC ";


      <table class="table table-bordered order-table ">
        <?php  if ($resultado = mysqli_query($enlace, $consulta)) { ?>
        <?php foreach ($resultado as $value){ ?>
              <td><?= $value['id']; ?></td>
              <td><?= $value['product']; ?></td>
              <td><?= $value['price']; ?></td>
              <td><?= $value['stock']; ?></td>
        <?php } ?>
        <?php mysqli_free_result($resultado); ?>
        <?php }else{ ?>
          <td colspan="3">No data</td>
      <?php } ?>
      <?php  mysqli_close($enlace); ?>

    </div> <!-- /container -->
Display PHP7 MySQL data

Download source code

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *