dongyong5912 2016-06-27 11:49
浏览 11

服务器未返回SQL数据

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. If you click on let's say Post 1, the link generated by PHP would be '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:

 $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. Here's the code from the '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>
  • 写回答

1条回答 默认 最新

  • douan9541 2016-06-27 12:14
    关注

    Okay, I found where I made a mistake. I forgot to add \' at

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

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等