为什么不退出while循环呢?


Why won't this exit out the while loop?

我正试图退出这个外面而看,所以我的脚本可以发布到Wordpress.com博客。然而,即使我尝试使用break;在if语句中,循环继续。

该函数启动脚本并基本处理发布:

function getFlogArticle($url, $mail) {  
list($id, $title, $content, $tags) = getNewArticle();
while ($id != 0)
{
    $start = getTime(); 
    doesArticleExist($url, $id);
        if ($exist = 0)
            {
                wordpress($title, $content, $mail, $tags, $url, $id);
                break;  
                $end = getTime(); 
                echo  '<strong>Exist While</strong>: '.round($end - $start,4).' seconds<br />'; 
            }
    list($id, $title, $content, $tags) = getNewArticle();   
    echo 'I cant stop'; 
}
}

每当doesARticleExist()返回1时,该函数就从数据库中获取文章:

function getNewArticle() {
$start = getTime(); 
global $db;
$count = $db->query("SELECT * FROM flog_articles");
$count = $count->num_rows;
$offset = mt_rand(0, $count - 1);
$stmt = "SELECT * FROM flog_articles LIMIT 1 OFFSET $offset";
$result = $db->query($stmt);
$post = $result->fetch_array(MYSQLI_ASSOC);
return array($post['article_id'], $post['article_title'], $post['article_content'], $post['article_keyword']);
$end = getTime(); 
echo  '<strong>getNewArticle()</strong>: '.round($end - $start,4).' seconds<br />';
}

这个脚本检查文章是否存在于数据库中。如果没有,它返回0。如果是,则返回1。

function doesArticleExist($url, $id) {
$start = getTime(); 
global $db; 
$count = $db->query("SELECT * FROM flog_posted WHERE http = $url AND article_id = $id");
$count = $count->num_rows;
if ($count > 0) {
    $exist = 1;
    return $exist;
} else{
    $exist = 0;
    return $exist;
}
$end = getTime();
echo  '<strong>doesArticleExist()</strong>: '.round($end - $start,4).' seconds<br />';
}

基本上,脚本从数据库获取一篇文章。获取文章后,它检查该文章/url组合是否存在于同一数据库的另一个表中。如果它不存在,我想让它发布到wordpress博客,然后跳出循环,这样它就不会再发布了。

唯一的问题是它甚至没有退出循环。是因为存在值没有被传递吗?

使用==进行比较。您正在做的是将0分配给$exist,这将始终失败的if语句。

不要使用break。使用这个

$willStop=false;
while (($id != 0)&&(!$willStop))
{
    $start = getTime(); 
    doesArticleExist($url, $id);
    if ($exist == 0)
        {
            wordpress($title, $content, $mail, $tags, $url, $id);
            $willStop=true;  
            $end = getTime(); 
            echo  '<strong>Exist While</strong>: '.round($end - $start,4).' seconds<br />'; 
        }
    list($id, $title, $content, $tags) = getNewArticle();   
    echo 'I cant stop'; 
}

所以在你的代码中有很多东西可以改进,看看下面的代码作为一个建议:

function getFlogArticles($url, $mail) {
    $list = getNewArticles();
    foreach($list as $article_id => $article) {
        wordpress($article['title'],$article['content'],$mail,$article['tags'],$url,$article_id);
    }
}
function getNewArticles($url) {
    global $db;
    $stmt = "SELECT article_id AS id,article_title AS title,article_content AS content,article_keyword AS tags FROM flog_articles";
    $result = $db->query($stmt);
    $articles = array();
    while($row = $result->fetch_array(MYSQLI_ASSOC)) {
        $articles[$row['id']] = $row;
    }
    if(empty($articles)) return array();
    $idlist = implode(',',array_keys($articles));
    //$url should be escaped as per your database type (eg, mysql_real_escape_string)
    $exists = array();
    $result = $db->query("SELECT article_id AS id FROM flog_posted WHERE http = '$url' AND article_id IN ($idlist)");
    while($row = $result->fetch_array(MYSQLI_ASSOC)) {
        $exists[$row['id']] = 1;
    }
    $articles = array_intersect_key($articles,$exists);
    return $articles;
}

有许多改进,具体来说,你只需要对数据库进行2次调用来返回你需要的所有行,而不是为你希望处理的每个新的有效文章进行3次查询。

如果您的数据库每次运行返回数千行,那么在某种程度上,您可能最好按照最初的方式执行,但是运行数百个非常小的查询的开销,在我的经验中,比运行一个或两个更大的查询更昂贵的cpu开销。

(我现在不能测试代码,所以如果它不能工作或做你需要它的正确的开箱即用的道歉)

希望有帮助