1

我试图获得小于我最后一个分页值的值,但我不断得到空数组,即使那里应该有值。是我的语法导致了这个还是我说错了?先感谢您。

 <?php
    include("connect.php"); //connect to database
        //create query

        $get_messages_query = $db->prepare("
            SELECT * FROM `blogposts`
            WHERE `postid` < ?
            ORDER BY `postid` DESC 
            LIMIT 5
            ");
        //Homepage should have last 5 blog posts this will take the 
        //last 5 entered into the database and put them into an 
        //array
    $one = 1;
$zero = 0;

$lastpage = $_GET['id'];

$pageback = $lastpage - 1;
$pageforward = $lastpage + 1;


        $get_messages_query->execute(array($_POST['idid']));
while($row = $get_messages_query->fetch())
    {
        if($row['comments'] == $one){
            $id = $row['postid'];
        $blog_post_history .=
        '<div class="post" id="post">
        <h1>' . $row['title'] .' </h1>
        <h2>Written by: ' . $row['author'] . '</h2>
        <p>'. $row['content'] . '</p>
        <a href="addcomment.php?id='. $row['postid'] .'">Add Comment</a>
        <a href="showcomments.php?id='. $row['postid'] . '">Show Comments</a>
        <br>
        <br>
    </div>';
}
    else{
        $id - $row['postid'];
        $blog_post_history .=
      '<div class="post" id="post">
       <h1>'. $row['title'] .' </h1>
        <h2>Written by: ' . $row['author'] . '</h2>
       <p>'. $row['content'] . '</p>
       <a href="viewblog.php?id='. $row['postid'] .'">View Blog</a>

       <br>
       <br>
  </div>';
    }

}
4

1 回答 1

0

确保$_POST['idid']包含您认为的整数值。例如,如果您与 POST 参数的名称不匹配,或者该值是字符串而不是数字,则您可能正在搜索postid < 0

启用 MySQL常规查询日志,您可以在执行时查看最终的 SQL(包括动态参数)。这是验证 SQL 是否按预期运行的简单方法。

当然,仅在数据库的开发或暂存实例上执行此操作,因为在生产站点上启用常规查询日志会对性能产生很高的影响。

还要在 mysql 客户端中测试查询,看看结果是否符合您的想法。您说“应该”有数据,但值得通过直接查询进行仔细检查,即在不涉及 PHP/PDO 的情况下进行测试。

于 2013-05-06T16:33:02.747 回答