2013-05-06 66 views
1

我想让值小于我上次的分页值,但我仍然得到空数组,即使应该有值。是我的语法造成这种情况还是我调用这个错误?先谢谢你。试图让值小于使用pdo

<?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>'; 
    } 

} 
+0

你能告诉你如何试图访问你的结果集吗? – 2013-05-06 16:26:36

+0

添加为喜爱 – Chris 2013-05-06 16:28:41

+1

我看不到这里的分页信息。通常这可以通过做'LIMIT 0,5'(第一页),'LIMIT 5,5'(第二页)等等来执行。所以你在这里有一些复杂的方法。我也不知道你在哪里试图把这些数据放到一个数组中,所以我不完全确定你提到的问题是什么。 – 2013-05-06 16:34:51

回答

0

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

启用MySQL general query log并且您可以查看最终的SQL - 包括动态参数 - 当它被执行时。这是一种简单的方法来验证SQL是否正在运行您所期望的。

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

也在mysql客户端测试查询,看看结果是否是你认为他们应该是。你说有“应该”是数据,但值得用直接查询进行仔细检查,即做一个没有涉及PHP/PDO的测试。

+0

Idid包含值9,并且在博客帖子中还有6个条目比这更少 – Chris 2013-05-06 16:36:33