php - Fetch Data from DB and Show it -


i have simple database post , need fetch title,image , user post.

my problem how loop data , represent each post on html? data null when call on html file.

html

<div class="container-fluid">   <div class="post">     <?php include './php/getmemes.php'; ?>                               <h1><?php $title; ?></h1>                     <img src="data:image/gif;base64, .'<?php $image; ?>'" >     <div class="postref" >       <button type="button" class="btn btn-success"><span class="glyphicon glyphicon-thumbs-up"></span></button>       <button type="button" class="btn btn-danger"><span class="glyphicon glyphicon-thumbs-down"></span></button>       <button type="button" class="btn btn-link"><?php //insert comments ?> comments</button>       <button type="button" class="btn btn-link"><?php $user; ?></button>     </div>   </div> </div> 

getmemes.php

<?php //database $servername = "localhost"; $username = "root"; $password = "";  // create connection $conn = new mysqli($servername, $username, $password); // check connection if ($conn->connect_error) {   die("connection failed: " . $conn->connect_error); }   $sql = "select title,image,user websitephp.posts"; $result = $conn->query($sql);  if($result->num_rows > 0) {   while($row = $result->fetch_assoc())    {                $title = $row["title"];     $image = $row["image"];     $user = $row['user'];   } } else  {   echo "0 results"; }  $conn->close(); 

i wanted avoid echo html inside while loop.

you need echo post's html inside of while loop, outside of it's scope variables aren't set.

<html> <head> ... </head> <body>  <!-- html posts -->  <?php //database $servername = "localhost"; $username = "root"; $password = "";  // create connection $conn = new mysqli($servername, $username, $password); // check connection if ($conn->connect_error) {     die("connection failed: " . $conn->connect_error); }   $sql = "select title,image,user websitephp.posts"; $result = $conn->query($sql);  if($result->num_rows > 0) {     while($row = $result->fetch_assoc())      {                    $title = $row["title"];         $image = $row["image"];         $user = $row['user'];         ?>        <div class="container-fluid">          <div class="post">             <?php include './php/getmemes.php'; ?>                                        <h1><?php echo $title; ?></h1>                              <img src="data:image/gif;base64, .'<?php echo $image; ?>'" >              <div class="postref" >                  <button type="button" class="btn btn-success"><span class="glyphicon glyphicon-thumbs-up"></span></button>                  <button type="button" class="btn btn-danger"><span class="glyphicon glyphicon-thumbs-down"></span></button>                  <button type="button" class="btn btn-link"><?php //insert comments ?> comments</button>                  <button type="button" class="btn btn-link"><?php echo $user; ?></button>              </div>          </div>      </div>      <?php     } } else  {     echo "0 results"; }  $conn->close();  ?>  <!-- html after posts --> </body> </html> 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

.htaccess - ERR_TOO_MANY_REDIRECTS htaccess -