PHP While循环中的多个SQL If语句

时间:2014-06-30 03:41:30

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

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

我正在执行WHILE循环来执行多个if语句,而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!";
        }

    }

}

&GT;

1 个答案:

答案 0 :(得分:0)

我身上发生了大量错误,但是在hashtags表中有三个hashtags,只有tweet_tags表中有一个hashtags的行。在这一个上浪费了几个小时。

故事的道德,总是记录并检查错误!