【问题标题】:While loop not satisfying both conditionsWhile 循环不满足两个条件
【发布时间】:2015-06-18 02:36:31
【问题描述】:

我有一个脚本可以从数据库中获取信息,并使用 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 时它不为空。
这是为什么呢?

【问题讨论】:

  • 为什么在循环之前执行与在循环内执行相同的查询?必须有一个 JOIN 查询,您可以这样做会阻止您当前尝试执行的 4 个查询。
  • 第一个查询是获取初始变量以在循环中进行检查。我会调查 JOIN 查询,谢谢。

标签: php while-loop conditional-statements


【解决方案1】:

我想是因为你在使用

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

在程序进入while循环之前。所以 $adDescription != '' 并且程序不会进入循环。

【讨论】:

  • 我不能说我很确定你的意思。您能否举例说明应该放入哪些内容?
  • 您正在为 $adDescription 分配一个值,因此它不是 == ' ' 并且程序不会进入 while 循环。我认为您应该尝试从 while 循环中删除 $adDescription == '' 作为条件,因为我实际上并不知道这种条件的原因。
  • 如果数据库中的“finished”列等于1,则$adDescription 的第一个值可能为空。如果 $adDescription 的值不为空,那么尽管不满足第二个条件,while 循环是否不会运行?
猜你喜欢
  • 2017-03-04
  • 1970-01-01
  • 1970-01-01
  • 2019-01-29
  • 2013-11-22
  • 1970-01-01
  • 2020-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多