从数据库中获取数据并显示它

时间:2017-07-28 08:36:26

标签: php

我有一个简单的数据库post,我需要获取该帖子的所有标题,图片和用户。

我的问题是我如何循环所有数据并代表html上的每个帖子?当我在HTML文件上调用它时,数据为空。

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 FROM 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();

我想避免回显while循环中的所有HTML。

3 个答案:

答案 0 :(得分:0)

您可以将结果集存储在while循环中的数组中

<?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 FROM websitephp.posts";
$result = $conn->query($sql);

if($result->num_rows > 0)
{
    $dataarray = array();
    while($row = $result->fetch_assoc()) 
    {           
        $dataarray[] = $row;

    }
}
else 
{
    echo "0 results";
}

$conn->close();

?>

使用foreach循环打印HTML中的结果

<?php include './php/getMemes.php'; 
foreach($dataarray as $data){ ?>
<div class="container-fluid">
 <div class="post">
     <h1><?php echo $data['title']; ?></h1>                
     <img src="data:image/gif;base64, .'<?php echo $data['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 $data['user']; ?></button>
     </div>

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

答案 1 :(得分:0)

您可以轻松地在HTML中使用PHP代码而无需回显所有内容。只需关闭php close标签func allItems() -> [TodoItem] { let todoDictionary = UserDefaults.standard.dictionary(forKey: ITEMS_KEY) as? [String:TodoItem] ?? [:] let items = Array(todoDictionary.values) return items.map({TodoItem(deadline: $0.deadline, title: $0.title, UUID: $0.UUID)}).sort({(left: TodoItem, right:TodoItem) -> Bool in (left.deadline.compare(right.deadline) == .orderedAscending) }) } ,插入你的html,并在你要打印的php块中回显php代码。循环中的所有内容都重复循环,直到条件完成。

以下是一个例子:

?>

答案 2 :(得分:-1)

您需要在while循环中回显帖子的HTML,在其范围之外,变量不会被设置。

<html>
<head>
...
</head>
<body>

<!-- Your HTML up to your 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 FROM 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();

?>

<!-- your HTML after your POSTS -->
</body>
</html>