PHP不打印整个表格

时间:2016-01-19 20:46:43

标签: php mysql

所以我试图创建一个准系统博客,其中它打印了mySQL表中的所有内容,但它似乎只打印第一行。我有两个PHP代码块,它是第二个。

正如您在代码中看到的那样,我首先在一个单独的块中打印最新的行,这样如果您还知道如何跳过表中第二个块的最新行,这也很不错。 / p>

提前致谢!

<?php
include_once"connect.php"; //Inkluderar connect.php filen

$sql = "SELECT * FROM posts";
$result = $conn->query($sql);
?>

<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="stylesheet" type="text/css" href="css/post-style.css">
<link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:600,400,300|PT+Sans:400,700|PT+Serif|PT+Sans+Narrow' rel='stylesheet' type='text/css'>
<meta name="viewport" content="width=device-width">
</head>

<body>
<?php
    include_once"header.php";

    //Väljer all data från angivna tabellen
    $sql = "SELECT * FROM posts ORDER BY id DESC LIMIT 1";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        //Skriver ut data från varje rad
        while($row = $result->fetch_assoc()){
            echo '<div id="newest" style="background-image: url(img/' .$row['img']. ');">';
            echo '<div id="newest-text">';
            echo '<h1 class="title">' . $row['title'] . '</h1>';
            echo '<h3 class="subtitle">' . $row['subtitle'] . '</h3>';
            echo '<a class="fullPost" href="post.php?id=' . $row['id'] . '"><i>Read More</i></a>';
            echo '</div></div>';
        }
    };
 ?>

<div id="content">
    <?php
    //Väljer all data från angivna tabellen
    $sql = "SELECT * FROM posts ORDER BY id";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        //Skriver ut data från varje rad
        while($row = $result->fetch_assoc())
            {
            echo '<div class="post">';
            echo "<div class='img'><img src=".'img/'.$row['img']." /></div>";
            echo '<h1 class="title">' . $row['title'] . '</h1>';
            echo '<h3 class="subtitle">' . $row['subtitle'] . '</h3>';
            $words = split(" ", $row['content']);
            for ($i = 0; ($i < 50 && $i < count($words)); $i++) {
                echo $words[$i] . " ";
            }
            echo '<br><a class="fullPost" href="post.php?id=' . $row['id'] . '"><i>Read More</i></a>';
            echo '<div class="line-separator"></div>';
            echo '<p class="tag"> Topic - ' . $row['tag'] . '</p>';
            echo '<p class="date">' . $row['date'] . '</p>';
            echo '</div>';
            }
       };
     ?>
</div>

3 个答案:

答案 0 :(得分:5)

  

但它似乎只打印第一行

查看您的查询:

SELECT * FROM posts ORDER BY id DESC LIMIT 1

LIMIT 1

LIMIT子句用于指定要返回的记录数,删除它以获取所有记录。

答案 1 :(得分:0)

您的SQL有LIMIT子句,这意味着只返回1行(在这种情况下是最新的行)。

如果您增加限制或删除限制,您的代码应显示所有这些行。

答案 2 :(得分:0)

由于您查询同一个表3次,最好只查询一次:

<?php
include_once"connect.php"; //Inkluderar connect.php filen

$sql = "SELECT * FROM posts ORDER BY id DESC";
$result = $conn->query($sql);

#just for debug
echo 'result->num_rows  = '.$result->num_rows;
?>

<html>
<head>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <link rel="stylesheet" type="text/css" href="css/post-style.css">
    <link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:600,400,300|PT+Sans:400,700|PT+Serif|PT+Sans+Narrow' rel='stylesheet' type='text/css'>
    <meta name="viewport" content="width=device-width">
</head>

<body>
<?php

if ($result->num_rows > 0) {
    //Skriver ut data från varje rad
    if($row = $result->fetch_assoc()){
        echo '<div id="newest" style="background-image: url(img/' .$row['img']. ');">';
        echo '<div id="newest-text">';
        echo '<h1 class="title">' . $row['title'] . '</h1>';
        echo '<h3 class="subtitle">' . $row['subtitle'] . '</h3>';
        echo '<a class="fullPost" href="post.php?id=' . $row['id'] . '"><i>Read More</i></a>';
        echo '</div></div>';
    }
};
?>

<div id="content">
    <?php
    if ($result->num_rows > 0) {
        //Skriver ut data från varje rad
        while($row = $result->fetch_assoc())
        {
            echo '<div class="post">';
            echo "<div class='img'><img src=".'img/'.$row['img']." /></div>";
            echo '<h1 class="title">' . $row['title'] . '</h1>';
            echo '<h3 class="subtitle">' . $row['subtitle'] . '</h3>';
            $words = split(" ", $row['content']);
            for ($i = 0; ($i < 50 && $i < count($words)); $i++) {
                echo $words[$i] . " ";
            }
            echo '<br><a class="fullPost" href="post.php?id=' . $row['id'] . '"><i>Read More</i></a>';
            echo '<div class="line-separator"></div>';
            echo '<p class="tag"> Topic - ' . $row['tag'] . '</p>';
            echo '<p class="date">' . $row['date'] . '</p>';
            echo '</div>';
        }
    }
    ?>
</div>

每次调用fetch_assoc()时,db光标会向前移动1条记录,直到没有更多行