php - 试图获得小于使用 pdo 的值

标签 php mysql

我试图获取小于上一个分页值的值,但我一直得到空数组,即使那里应该有值。是我的语法导致了这个还是我说错了?提前谢谢你。

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

}

最佳答案

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

启用 MySQL general query log您可以在执行时查看最终的 SQL(包括动态参数)。这是一种验证 SQL 是否按预期运行的简单方法。

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

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

关于php - 试图获得小于使用 pdo 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16402941/

相关文章:

mysql - 如何从MySQL中的特殊表结构中查询信息?

python - 在django中手动创建中间表

php - 替代地穴()

php - 使用 PHP 从 MySQL 数据库中提取 ID 号

php - Laravel 5.1 测试 - 从数据库获取行以完成测试

php - 使用 Doctrine 2 获取最后一个插入 id?

mysql - 将 SELECT 语句更改为 DELETE

mysql - SQL菜鸟。 "Foreign Key Constraint is incorrectly formed"

mysql - 如何选择和更新涉及两个表的查询

phpunit 不能在 Windows 上工作