php在准备好的语句中循环不成功

时间:2013-04-22 19:34:38

标签: php while-loop prepared-statement prepare

下面的

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

  1. 如果文章没有批准的评论,则lastmod是批准 文章的日期。
  2. 如果文章至少有1个已批准的评论,那么lastmod就是 最后批准的评论的批准日期。
  3. 输出中的错误是:

    1. 我的数据库中有一些文章没有评论,但这些无评论的文章获得了其他一些文章的最后评论的批准日期。因此,由于我今天已经批准了一些评论,所有那些无评论文章的“lastmode”都是今天的日期。但这些文章很老了。
    2. 在我的第二个while循环中
    3. $newsql打印“newstmt prepare error” 屏幕,因此if ($newstmt = $connection->prepare($newsql))部分不起作用
    4. 你能纠正我吗?

      谢谢,最好的问候

      <?php       
      //if spesific article is approved then its last modification date = approval date of article
      //if spesific article has approved comment(s), then modification date = approval date of its last comment
      $sql = "SELECT col_author, col_title, col_approvaldate FROM articles WHERE col_status = ? ORDER by col_approvaldate DESC";
      
      if ($stmt = $connection->prepare($sql)) 
      {
          /* bind parameters */
          $stmt -> bind_param("s", $bindparam1);
      
          /* assign value */
          $bindparam1 = 'approved';
      
          /* execute statement */
          $stmt->execute();
      
          /* bind result variables */
          $stmt->bind_result($author, $title, $articledate);
      
          /* fetch values */
          while ($stmt->fetch()) 
          {
              //if exist, get approved newest comment approval date
              $newsql = "SELECT col_approvaldate FROM comments WHERE col_status = ? AND col_for_author = ? AND col_for_title = ? ORDER by col_approvaldate DESC LIMIT 1";
              if ($newstmt = $connection->prepare($newsql))
              {
                  /* bind parameters */
                  $newstmt -> bind_param("sss", $ybindparam1, $ybindparam2, $ybindparam3);
      
                  /* give values */
                  $ybindparam1 = 'approved';
                  $ybindparam2 = $author;
                  $ybindparam3 = $title;
      
                  /* execute statement */
                  $newstmt->execute();
      
                  /* bind result variables */
                  $newstmt->bind_result($commentdate);
      
                  /* fetch values */
                  $biggerdate = '';
      
                  while ($newstmt->fetch())
                  {                   
                      // is there any approved comment for this article?
                      if (!is_null($commentdate))
                      {$biggerdate = $commentdate;}
                  }
                  /* close statement */
                  $newstmt->close();                  
              }
              else {echo 'newstmt prepare error';}
      
              //print the result
              echo '<url>'."\r\n";
              echo "\t".'<loc>'.root_folder.urlencode('articles').'/'.urlencode(space_to_dash($author)).'/'.urlencode(space_to_dash(no_punctuation($title))).'</loc>'."\r\n";             
              //if there is no approved comment for this article
              if ($biggerdate == '') 
              {
                  $biggerdate = $articledate;
              }
              $datetime = new DateTime($biggerdate);
              $biggerdate = $datetime->format('Y-m-d\TH:i:sP');
              echo "\t".'<lastmod>'.$biggerdate.'</lastmod>'."\r\n";
              echo '</url>'."\r\n";
      
          }
          /* close statement */
          $stmt->close();
      
      }
      ?>
      

1 个答案:

答案 0 :(得分:1)

它必须如何

  1. 您必须使用单个查询获取数据。
  2. 必须使用文章ID
  3. 关联评论
  4. PDO而不是mysqli应该被使用
  5. 所以,你走了:

    $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";
    }
    

    它当然没有经过测试,显然包含很多错误,只是为了向您展示一个想法。

    首先,你必须进行查询工作 然后制作读取链接的PDO标签wiki并建立连接 然后修复所有错误和拼写错误,如果有的话