php在准备好的语句中循环时失败

php在准备好的语句中循环时失败,php,while-loop,prepared-statement,prepare,Php,While Loop,Prepared Statement,Prepare,下面的代码是我的sitemap.xml文件的一部分。我的目标是写我批准的文章的最后修改日期。此数据有两种可能 如果文章没有经批准的评论,则批准为lastmod 文章日期 如果文章至少有1条已批准的评论,则lastmod是 上次批准评论的批准日期 我的输出中的错误有: my db中有一些文章没有评论,但这些没有评论的文章获得了一些其他文章最后评论的批准日期。因此,由于我今天批准了一些评论,所有这些无评论文章的最后模式都是今天的日期。但是这些东西都很旧了 $newsql在我的第二个while循环中打

下面的代码是我的
sitemap.xml
文件的一部分。我的目标是写我批准的文章的最后修改日期。此数据有两种可能

  • 如果文章没有经批准的评论,则批准为
    lastmod
    文章日期
  • 如果文章至少有1条已批准的评论,则
    lastmod
    是 上次批准评论的批准日期
  • 我的输出中的错误有:

  • my db中有一些文章没有评论,但这些没有评论的文章获得了一些其他文章最后评论的批准日期。因此,由于我今天批准了一些评论,所有这些无评论文章的最后模式都是今天的日期。但是这些东西都很旧了
  • $newsql
    在我的第二个while循环中打印“newstmt准备错误” 屏幕,所以
    if($newstmt=$connection->prepare($newsql))
    part不能工作
  • 你能纠正我吗

    谢谢你,问好

    代码
    
    
    它必须是怎样的

  • 您必须使用单个查询获取数据
  • 评论必须使用文章id链接
  • 应该使用mysqli而不是mysqli
  • 那么,给你:

    $sql = "SELECT author, title, a.approvaldate, max(c.approvdate) biggerdate
            FROM articles a
            LEFT JOIN comments c ON c.article_id = a.id AND c.status='approved'
            WHERE a.status = 'approved' 
            GROUP BY a.id
            ORDER BY a.approvaldate DESC";
    $stmt = $con->prepare($sql);
    $stmt->execute();
    $data = $stmt->fetchall();
    foreach ($data as $row) {
        extract($row);
        echo "<url>\r\n";
        echo "\t<loc>".root_folder.'articles';
        echo '/'.urlencode(space_to_dash($author));
        echo '/'.urlencode(space_to_dash(no_punctuation($title)))."</loc>\r\n";
        //if there is no approved comment for this article
        if (!$biggerdate) 
        {
            $biggerdate = $approvaldate;
        }
        $datetime = new DateTime($biggerdate);
        $biggerdate = $datetime->format('Y-m-d\TH:i:sP');
        echo "\t<lastmod>$biggerdate</lastmod>\r\n";
        echo "</url>\r\n";
    }
    
    $sql=“选择作者、标题、a.approvaldate、最大(c.approvdate)biggerdate
    从a条
    在c.article_id=a.id和c.status='approved'上左连接注释c
    其中a.status=‘已批准’
    按a.id分组
    由a.approvaldate DESC订购”;
    $stmt=$con->prepare($sql);
    $stmt->execute();
    $data=$stmt->fetchall();
    foreach($行数据){
    摘录(行);
    回显“\r\n”;
    echo“\t”.root_文件夹'articles';
    echo“/”.urlencode(空格到破折号($author));
    echo“/”.urlencode(空格到破折号(无标点($title))。“\r\n”;
    //如果对本文没有批准的评论
    如果(!$biggerdate)
    {
    $biggerdate=$approvaldate;
    }
    $datetime=新日期时间($biggerdate);
    $biggerdate=$datetime->format('Y-m-d\TH:i:sP');
    echo“\t$biggerdate\r\n”;
    回显“\r\n”;
    }
    
    它当然没有经过测试,显然包含许多错误,但只是为了向您展示一个IDE

    首先,您必须使查询工作正常。
    然后创建读取链接的PDO标记wiki并建立连接。

    然后修复所有错误和打字错误(如果有)

    从col_status=的注释中选择col_approvaldate是什么?和col_for_author=?和col_表示_title=?ORDER by col_approvaldate DESC LIMIT 1在直接向db执行时(如phpMyAdmin或类似)提供的信息嘿,你没有使用文章id将评论链接到文章?真的吗?@bestprogrammerintheworld它给了我语法错误。我完全按照你写的那样做了,但我不习惯直接使用phpmyadmin查询窗口。@你的常识是的,这可能是愚蠢的方式,但我不使用id。打印
    $connection->error
    以及
    新闻TMT prepare error
    消息来查看错误原因。我会按照你说的方式研究它。但我想问的是,我的评论表中没有文章id作为外键;那么这是否意味着即使你的代码100%正确,我也不能使用它?你只需要在articles表中有一个article id,在comments表中有相应的article id。这就是全部
    $sql = "SELECT author, title, a.approvaldate, max(c.approvdate) biggerdate
            FROM articles a
            LEFT JOIN comments c ON c.article_id = a.id AND c.status='approved'
            WHERE a.status = 'approved' 
            GROUP BY a.id
            ORDER BY a.approvaldate DESC";
    $stmt = $con->prepare($sql);
    $stmt->execute();
    $data = $stmt->fetchall();
    foreach ($data as $row) {
        extract($row);
        echo "<url>\r\n";
        echo "\t<loc>".root_folder.'articles';
        echo '/'.urlencode(space_to_dash($author));
        echo '/'.urlencode(space_to_dash(no_punctuation($title)))."</loc>\r\n";
        //if there is no approved comment for this article
        if (!$biggerdate) 
        {
            $biggerdate = $approvaldate;
        }
        $datetime = new DateTime($biggerdate);
        $biggerdate = $datetime->format('Y-m-d\TH:i:sP');
        echo "\t<lastmod>$biggerdate</lastmod>\r\n";
        echo "</url>\r\n";
    }