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

Php 从数据库中获取数据并显示它,php,Php,我有一个简单的数据库帖子,我需要获取该帖子的所有标题、图片和用户 我的问题是如何循环所有数据并在html上表示每个帖子?当我在HTML文件中调用它时,数据为空 HTML getMemes.php <?php //DATABASE $servername = "localhost"; $username = "root"; $password = ""; // Create connection $conn = new mysqli($servername, $username, $pas

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

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

HTML

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。

您需要在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>

可以将结果集存储在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 } ?> 

您可以轻松地在html中使用php代码,而无需重复所有内容。只需使用php close tag?>,插入html,并在要打印的php块中回显php代码即可。在循环过程中,重复所有操作,直到条件完成

以下是一个例子:

<?php
$conn = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
$sql  = "SELECT * FROM tbl_name where ...";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) { ?>

<div class="some_class">Title: <?php echo $row['title']; ?> </div>
<?php }

这是行不通的,原因有几个。首先,在每个新条目之后覆盖while循环中的所有变量。使用数组执行此操作。另外,在while循环外部定义数组,否则作用域仅在while循环内部,变量在循环外部不可访问。或者简单地将HTML部分放在while循环中。在HTML中无论如何都需要一个循环,否则这是不可能的。但是如何在HTML中循环数组呢?例如,简单地使用一个循环,在这里使用foreach循环将是一件好事。为什么要避免在while循环中回显它?您需要一个循环来打印所有帖子!此外,您还需要在php代码中使用echo函数来打印值。示例注释,问题的最后一行:我想避免在while循环中回显所有的HTML。但是数据还是给我空的。但在你推我的朋友之前它就起作用了!在您的时间内,无需阵列推送。我已经编辑了他的答案,只需要复习一下$dataarray[]=$row;这完全够了。
<?php
$conn = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
$sql  = "SELECT * FROM tbl_name where ...";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) { ?>

<div class="some_class">Title: <?php echo $row['title']; ?> </div>
<?php }