PHP Mysqli查询检索的值多于应有的值

时间:2016-03-04 10:54:31

标签: php if-statement mysqli while-loop

这是我在这里发表的第一篇文章,我目前在空闲时间学习php,我正在尝试开发一个应用程序,我遇到了一个我无法找到解决方案的错误。在我发布的代码中,当条件到达此部分时:

elseif ($result2['ODT'] == '0'){
                            $query3 = mysqli_query($dbconfig, "SELECT po.texto, po.nextp FROM paginas p, pagsop po, usergame u WHERE u.userGID = '".$_SESSION['login_user']."' and po.id != '10' and po.id != '7' and p.np=u.uap and po.np = p.np");
                            $row1 =mysqli_fetch_assoc($query3);
                            echo '<option value="'.$row1['nextp'].'">'.$row1['texto'].'</option>';}

它给了我2个值,2个选择选项,但如果我直接在phpmyadmin上运行它,它只检索我一行。我在那里做错了什么? 正确的是1行,而不是2,我认为它必须是关于while的东西。提前致谢

function Select1() {
    header("Content-Type: text/html;  charset=ISO-8859-1",true);

    include 'dbconfig.php';

    $query = mysqli_query($dbconfig, "SELECT p.textop, p.np, p.ap, po.texto, po.nextp, u.uap FROM paginas p, pagsop po, usergame u WHERE u.userGID = '".$_SESSION['login_user']."' and p.np=u.uap and po.np = p.np");
    $row_cnt = mysqli_num_rows($query);

    if($row_cnt > 0){
                while($row =mysqli_fetch_assoc($query)){
                    $query1 = mysqli_query($dbconfig, "SELECT uap FROM usergame WHERE userGID = '".$_SESSION['login_user']."'");
                    $result = mysqli_fetch_assoc($query1);
                    if(($result['uap'] !== '230')and($result['uap'] !== '20')){
                    echo "<option value='".$row['nextp']."'>".$row['texto']."</option>";}

                    elseif(($result['uap'] == '230')or($result['uap'] == '20')){
                    $query2 = mysqli_query($dbconfig, "SELECT ODT FROM userencantos WHERE userEID = '".$_SESSION['login_user']."'");
                    $result2 = mysqli_fetch_assoc($query2);
                        if ($result2['ODT'] > '0'){echo '<option value="'.$row['nextp'].'">'.$row['texto'].'</option>';}
                        elseif ($result2['ODT'] == '0'){
                            $query3 = mysqli_query($dbconfig, "SELECT po.texto, po.nextp FROM paginas p, pagsop po, usergame u WHERE u.userGID = '".$_SESSION['login_user']."' and po.id != '10' and po.id != '7' and p.np=u.uap and po.np = p.np");
                            $row1 =mysqli_fetch_assoc($query3);
                            echo '<option value="'.$row1['nextp'].'">'.$row1['texto'].'</option>';}
                        }
                }
            }else{
                echo '<option value="">Nada foi encontrado na BD</option>';
            }
};

2 个答案:

答案 0 :(得分:0)

您是否尝试将此部分从“或”更改为“和”?

elseif(($result['uap'] == '230')or($result['uap'] == '20')){

elseif(($result['uap'] == '230')and($result['uap'] == '20')){

答案 1 :(得分:0)

抱歉,伙计们,但我自己解决了:)我改变了代码,现在它完美无缺。

function Select1() {
header("Content-Type: text/html;  charset=ISO-8859-1",true);

include 'dbconfig.php';

$query = mysqli_query($dbconfig, "SELECT uap FROM usergame WHERE userGID     = '".$_SESSION['login_user']."'");
$result = mysqli_fetch_assoc($query);

if(($result['uap'] !== '230')and($result['uap'] !== '20')){
$query1 = mysqli_query($dbconfig, "SELECT p.textop, p.np, p.ap, po.texto, po.nextp, u.uap FROM paginas p, pagsop po, usergame u WHERE u.userGID = '".$_SESSION['login_user']."' and p.np=u.uap and po.np = p.np");
$row_cnt = mysqli_num_rows($query1);
            while($row =mysqli_fetch_assoc($query1)){
                echo "<option value='".$row['nextp']."'>".$row['texto']."</option>";}
}

elseif(($result['uap'] == '230')or($result['uap'] == '20')){
$query2 = mysqli_query($dbconfig, "SELECT ODT FROM userencantos WHERE userEID = '".$_SESSION['login_user']."'");
$result2 = mysqli_fetch_assoc($query2);
    if ($result2['ODT'] > '0'){
        $query3 = mysqli_query($dbconfig, "SELECT p.textop, p.np, p.ap, po.texto, po.nextp, u.uap FROM paginas p, pagsop po, usergame u WHERE u.userGID = '".$_SESSION['login_user']."' and p.np=u.uap and po.np = p.np");
        $row_cnt1 = mysqli_num_rows($query3);
            while($row1 =mysqli_fetch_assoc($query3)){
                echo "<option value='".$row1['nextp']."'>".$row1['texto']."</option>";}
        }
    elseif ($result2['ODT'] == '0'){
        $query4 = mysqli_query($dbconfig, "SELECT po.texto, po.nextp FROM paginas p, pagsop po, usergame u WHERE u.userGID = '".$_SESSION['login_user']."' and po.id != '10' and po.id != '7' and p.np=u.uap and po.np = p.np");
        $row_cnt2 = mysqli_num_rows($query4);
            while($row2 =mysqli_fetch_assoc($query4)){
                echo "<option value='".$row2['nextp']."'>".$row2['texto']."</option>";}
        }
}

};