虽然循环不满足两个条件

时间:2015-04-12 20:23:07

标签: php while-loop conditional-statements

我有一个脚本从数据库中获取信息,并使用while循环检查某些条件。始终满足第一个条件,但第二个条件不符合 这是我正在使用的代码:

//Get the new ad's id
    $stmt = mysqli_prepare($db, "SELECT visits, description, url, views, time FROM paidAds WHERE id=? AND finished!=?");
    //Bind Items
    mysqli_stmt_bind_param($stmt, 'ii', $currentAd, $adFinished);
    //Execute statement
    mysqli_stmt_execute($stmt);
    //Bind password to variable
    mysqli_stmt_bind_result($stmt, $adVisits, $adDescription, $adUrl, $adViews, $adTime);
    //Fetch password
    mysqli_stmt_fetch($stmt);
    //Close statement
    mysqli_stmt_close($stmt);

    //Get the new ad's id
    $stmt2 = mysqli_prepare($db, "SELECT ip FROM adViews WHERE adId=? AND address=?");
    //Bind Items
    mysqli_stmt_bind_param($stmt2, 'is', $currentAd, $address);
    //Execute statement
    mysqli_stmt_execute($stmt2);
    //Bind password to variable
    mysqli_stmt_bind_result($stmt2, $userIp);
    //Fetch password
    mysqli_stmt_fetch($stmt2);
    //Close statement
    mysqli_stmt_close($stmt2);

    if($stmt2 === false){
        echo mysqli_error($db);
    }

    while($adDescription == '' && $userIp != ''){
        $currentAd += 1;

        //Get the new ad's id
        $stmt = mysqli_prepare($db, "SELECT visits, description, url, views, time FROM paidAds WHERE id=? AND finished!=?");
        //Bind Items
        mysqli_stmt_bind_param($stmt, 'ii', $currentAd, $adFinished);
        //Execute statement
        mysqli_stmt_execute($stmt);
        //Bind password to variable
        mysqli_stmt_bind_result($stmt, $adVisits, $adDescription, $adUrl, $adViews, $adTime);
        //Fetch password
        mysqli_stmt_fetch($stmt);
        //Close statement
        mysqli_stmt_close($stmt);

        //Get the new ad's id
        $stmt2 = mysqli_prepare($db, "SELECT ip FROM adViews WHERE adId=? AND address=?");
        //Bind Items
        mysqli_stmt_bind_param($stmt2, 'is', $currentAd, $address);
        //Execute statement
        mysqli_stmt_execute($stmt2);
        //Bind password to variable
        mysqli_stmt_bind_result($stmt2, $userIp);
        //Fetch password
        mysqli_stmt_fetch($stmt2);
        //Close statement
        mysqli_stmt_close($stmt2);

        if($userIp != ''){
            echo 'error';
        }
    }

    echo $userIp;

此脚本的输出不会显示任何“错误”,但在打印变量$ userIp时,它不是空的。
这是为什么?

1 个答案:

答案 0 :(得分:0)

我认为这是因为你正在使用

mysqli_stmt_bind_result($stmt, $adVisits, $adDescription, $adUrl, $adViews, $adTime);

程序进入while循环之前。所以$ adDescription!=''并且程序没有进入循环。