E
E
elo-dev2020-05-16 16:49:39
PHP
elo-dev, 2020-05-16 16:49:39

How to fetch data from database in php?

I tried to output like this, but nothing happens:

<div class="review-row">
  <?php
    $con=mysqli_connect("localhost","root","root","review");

    if (!$con) {
      die("Connection failed: " . mysqli_connect_error());
      }

    $result = mysqli_query("SELECT * FROM reviews");

    mysqli_close();

    while($row = mysqli_fetch_array($result)){
  ?>

      <div class="block-test">
      <div class="review">
        <div class="review-wrapper">
          <span class="review-item-text"><?php echo $row['content']?></span>
          <div class="review-item-order"><?php echo $row['email']?></div> 
          <div class="review-item-row">
            <div class="review-item-username"><?php echo $row['name']?></div>
            <div class="review-item-date">10 мая 2020, 17:06</div>
          </div>
        </div>
      </div>
      </div>
  <?php } ?>


Here is the db:5ebfef4d7725c263179875.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
FanatPHP, 2020-05-16
@elo-dev

You never have to ask "how to do this and that" if you obviously know how to do it, and have already done it .
It is necessary to ask, " how to figure out why it does not work ." To do this, follow the recommendations from here
In particular, before connecting to the database, write

ini_set('display_errors',1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

and then see what errors it gives you. And fix them.
And closing the connection can be simply removed from the code. It doesn't interfere, but it doesn't do any good either. especially since it's badly written.

A
AUser0, 2020-05-16
@AUser0

Migrate mysqli_close(); way down to the last line of the above code, inside <?php } ?> after the closing curly brace.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question