【问题标题】:Multiple SQL If statements within PHP While loopsPHP While 循环中的多个 SQL If 语句
【发布时间】:2014-08-20 09:12:23
【问题描述】:

我创建了一个脚本,以 cron 作业的形式每隔五分钟在我的数据库上运行。这不是一段写得很好的代码,但它完成得很快,现在应该可以完成这项工作。

我正在执行一个 WHILE 循环来执行多个 if 语句,这些语句中又包含多个 SQL 语句。问题是,它只迭代 WHILE 循环一次然后停止,我不完全确定为什么。代码如下:

<?php

require_once('config.php');

$hashtags = mysql_query("SELECT id, hashtag FROM hashtags WHERE enabled = '1'") or die(mysql_error());

while($row = mysql_fetch_array($hashtags))
{
    $hashtag_id = $row['id'];
    $hashtag = $row['hashtag'];


    //Get id and latest_tweet_id from report log
    $latest_report_tweet_id_query = mysql_query("SELECT id, latest_tweet_id FROM reports_log WHERE name = 'post_count' AND hashtag_id = '".$hashtag_id."' LIMIT 1") or die(mysql_error());

    if (mysql_num_rows($latest_report_tweet_id_query) == 0) { 
        $new_report_tweet_id_query = mysql_fetch_array(mysql_query("SELECT tweet_id FROM tweet_tags WHERE tag = '".$hashtag."' ORDER by tweet_id desc LIMIT 1")) or die(mysql_error());
        $new_report_tweet_id = $new_report_tweet_id_query['tweet_id'];  

        $post_count_query = mysql_fetch_array(mysql_query("SELECT count(tweet_id) as tweet_count FROM tweet_tags WHERE tag = '".$hashtag."' AND tweet_id <= '".$new_report_tweet_id."'")) or die(mysql_error());
        $post_count = $post_count_query['tweet_count'];

        if(mysql_query("INSERT INTO post_count_reports (timestamp, hashtag_id, post_count, latest_tweet_id) VALUES ('".date("Y-m-d H:i:s")."', '".$hashtag_id."', '".$post_count."', '".$new_report_tweet_id."')"))
        {
            //Get just created id of the report
            $report_id_query = mysql_fetch_array(mysql_query("SELECT id FROM post_count_reports WHERE hashtag_id = '".$hashtag_id."' AND latest_tweet_id = '".$new_report_tweet_id."'")) or die(mysql_error());
            $report_id = $report_id_query['id'];

            if(mysql_query("INSERT INTO reports_log (timestamp, hashtag_id, name, latest_tweet_id, latest_report_id) VALUES ('".date('Y-m-d H:i:s')."', '".$hashtag_id."', 'post_count', '".$new_report_tweet_id."', '".$report_id."')")) 
            {
                echo "Successfully created report! NEW";
            }
            else {
                echo "Failed updating report log! NEW";
            }
        }
        else
        {
            echo "Failed making report! NEW";
        }
    }
    else {

        //Set the latest report id
        $latest_report_tweet_id_array = mysql_fetch_array($latest_report_tweet_id_query);
        $latest_report_log_id = $latest_report_tweet_id_array['id'];  
        $latest_report_tweet_id = $latest_report_tweet_id_array['latest_tweet_id'];   

        //Query to get the latest tweet_id in the database        
        $new_report_tweet_id_query = mysql_fetch_array(mysql_query("SELECT tweet_id FROM tweet_tags WHERE tag = '".$hashtag."' ORDER by tweet_id desc LIMIT 1")) or die(mysql_error());
        $new_report_tweet_id = $new_report_tweet_id_query['tweet_id']; 

        //Query to get the new post count from database
        $new_post_count_query = mysql_fetch_array(mysql_query("SELECT count(tweet_id) as tweet_count FROM tweet_tags WHERE tag = '".$hashtag."' AND tweet_id > '".$latest_report_tweet_id."' AND tweet_id <= '".$new_report_tweet_id."'")) or die(mysql_error());
        $new_post_count = $new_post_count_query['tweet_count'];

        $old_post_count_query = mysql_fetch_array(mysql_query("SELECT id, post_count FROM post_count_reports ORDER by timestamp desc LIMIT 1")) or die(mysql_error());
        $old_post_count = $old_post_count_query['post_count'];

        $post_count = $old_post_count + $new_post_count;

        if(mysql_query("INSERT INTO post_count_reports (timestamp, hashtag_id, post_count, latest_tweet_id) VALUES ('".date('Y-m-d H:i:s')."', '".$hashtag_id."', '".$post_count."', '".$new_report_tweet_id."')"))
        {
            //Get just created id of the report
            $report_id_query = mysql_fetch_array(mysql_query("SELECT id FROM post_count_reports WHERE hashtag_id = '".$hashtag_id."' AND latest_tweet_id = '".$new_report_tweet_id."' ORDER by timestamp desc LIMIT 1")) or die(mysql_error());
            $report_id = $report_id_query['id'];

            if(mysql_query("UPDATE reports_log SET id = '".$latest_report_log_id."', timestamp = '".date('Y-m-d H:i:s')."', latest_tweet_id = '".$new_report_tweet_id."', latest_report_id = '".$report_id."' WHERE name = 'post_count'")) 
            {
                echo "Successfully created report!";
            }
            else {
                echo "Failed updating report log!";
            }
        }
        else
        {
            echo "Failed making report!";
        }

    }

}

?>

【问题讨论】:

  • 你怎么知道它只迭代一次?
  • 它返回“成功创建报告!新”或“成功创建报告!”一次。数据库中的“hashtags”表中有三行。我只能看到根据“主题标签”表中的第一行添加到报告中的一行 - 执行时。
  • 就在while 循环之前,把它放上去看看它会打印出什么echo mysql_num_rows($hashtags); die();
  • 它返回 3。我不确定是什么阻止了它再次迭代。

标签: php sql if-statement while-loop cron


【解决方案1】:

我的错误很大,结果表明,虽然 hashtags 表中有三个主题标签,但 tweet_tags 表中只有一个带有主题标签的行。在这个上浪费了几个小时。

故事的寓意,始终记录并检查错误!

【讨论】:

    猜你喜欢
    • 2013-11-01
    • 2017-03-09
    • 2015-05-19
    • 1970-01-01
    • 2016-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多