php unsuccessful while loop within prepared statements

﹥>﹥吖頭↗ 提交于 2019-12-25 04:07:26

问题


code below is a part of my sitemap.xml file. My aim is to write the last modified date of my approved articles. There are 2 possibilities for this data.

  1. If article has no approved comment, then lastmod is the approval date of the article.
  2. If article has at least 1 approved comment, then lastmod is the approval date of the last approved comment.

Errors in my output are:

  1. There are some articles in my db that has no comments but also these commentless articles get the approval date of last comment of some other articles. As a result since I have some comments which are approved today, all those commentless articles' lastmode is date of today. but these articles are quite old.
  2. $newsql in my 2nd while loop prints "newstmt prepare error" on screen, so if ($newstmt = $connection->prepare($newsql)) part doesn't work

Can you please correct me?

Thank you, best regards

code

<?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:


How it have to be

  1. You have to get your data using single query.
  2. Comments have to be linked using article id
  3. PDO instead of mysqli ought to be used

So, here you go:

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

it is of course not tested and apparently contains many errors but just to show you an ides.

First of all you have to make query work.
then make read linked PDO tag wiki and establish a connection.
Then fix all the errors and typos, if any



来源:https://stackoverflow.com/questions/16155260/php-unsuccessful-while-loop-within-prepared-statements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!