简体   繁体   English

服务器未返回SQL数据

[英]Server is not returning the SQL data

I made a simple blog script in PHP and SQL, on the page where the latest posts need to be displayed everything works fine, however, I made a page 'comments.php' that will load the comments of the selected post. 我用PHP和SQL制作了一个简单的博客脚本,在需要显示最新帖子的页面上一切正常,但是,我创建了一个页面'comments.php',它将加载所选帖子的评论。 If you click on let's say Post 1, the link generated by PHP would be 'comments.php?id=1'. 如果你点击帖子1,那么PHP生成的链接就是'comments.php?id = 1'。

This is the code I have on the 'blog.php' page, where everything works fine and post content is displayed correctly: 这是我在'blog.php'页面上的代码,其中一切正常,发布内容正确显示:

 $db = new PDO('mysql:hostname=localhost;dbname=test;charset=utf8', 'root', '');
$req = $db->query('SELECT id, title, content, DATE_FORMAT(creation, \'%d/%m/%Y at %H:%i\') AS datePosted, user FROM blogpost ORDER BY creation DESC LIMIT 0, 5');
    while($data = $req->fetch()){
      ?>
        <div class="blogpost-container">
          <div class="blogpost-title">

            <?php
                echo htmlspecialchars($data['title']) . " | <em>Posted on ".$data['datePosted']."</em>";
             ?>
          </div>
          <div class="blogpost-content">
            <?php
                echo nl2br(htmlspecialchars($data['content']));
            ?>
          </div>
          <div class="blogpost-user">
            <?php
                echo "Posted by: ".$data['user'].".";
             ?>
           </div>
           <div class="blogpost-comments">
             <a href=<?php echo "'comments.php?id=". $data['id']."'"; ?> >
               Show comments
             </a>
           </div>
        </div>
      <?php
    }
    $req->closeCursor();
 ?>

Here, the post load correctly, but once I go on comments.php?id=1 for example, it doesn't show anything. 在这里,post正确加载,但是一旦我继续使用comments.php?id = 1,它就没有显示任何内容。 Here's the code from the 'comments.php': 这是'comments.php'中的代码:

<?php
  $db = new PDO('mysql:hostname=localhost;dbname=test;charset=utf8;', 'root', '');
  $req = $db->prepare('SELECT id, title, content, DATE_FORMAT(creation, \'%d/%m/%Y at %H:%i), user FROM blogpost WHERE id = ?');
  $req->execute(array(
      $_GET['id']
  ));

  $data = $req->fetch();
 ?>
 <div class="blogpost-container">
   <div class="blogpost-title">

     <?php
         echo htmlspecialchars($data['title']) . " | <em>Posted on ".$data['datePosted']."</em>";
      ?>
   </div>
   <div class="blogpost-content">
     <?php
         echo nl2br(htmlspecialchars($data['content']));
     ?>
   </div>
   <div class="blogpost-user">
     <?php
         echo "Posted by: ".$data['user'].".";
      ?>
    </div>
 </div>

Okay, I found where I made a mistake. 好的,我发现了我犯了错误的地方。 I forgot to add \\' at 我忘了添加\\'

... DATE_FORMAT(creation, \'....\')....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM