使用if语句或while语句对照数据库检查当前项目值?

时间:2019-06-24 19:43:03

标签: php mysql if-statement while-loop

尝试检查某行是否具有某个字符串,如果有则执行一些操作。

我尝试在if语句中使用AND OR && ||来组合检查,但失败了,然后尝试了每一个。代码如下。

//QUERY ARRAY//
<?php
    $sarray = array("first", "second", "third", "fourth", "fifth", "sixth");
    $sArray2 = '"' . implode('","', $sarray) . '"';
    $query = "SELECT positions, id FROM tableone WHERE positions IN ($sArray2) ORDER BY id ASC";

    // id's are numbers from 1 - ~ number of rows ///
    $result = mysqli_query($con,$query);

    if (!$result) {
        die("Connection Error: " . mysqli_connect_error());
    }

    $count = mysqli_num_rows($result);
    $i = 1;

    while ($row = mysqli_fetch_assoc($result)) 
    {
        if($i%1==1)         
            if ($row['positions'] = "first") {
                echo $row['first'];
            } else if ($row['positions'] == "second") {
                echo $row['second'];
            } else if ($row['positions'] == "third") {
                echo $row['third'];
            } else if ($row['positions'] == "fourth") {
                echo $row['fourth'];
            } else if ($row['positions'] == "fifth" || "sixth") {
                echo $row['fifth'];
                echo $row['sixth'];
            }
        ?>

但是我也试图做这样的事情:

while ($row['positions'] == "first")
{
    echo $row['first'];
}

while ($row['positions'] == "second")
{
    echo $row['second'];    
}

while ($row['positions'] == "third")
{
    echo $row['third'];    
}

while ($row['positions'] == "fourth")
{
    echo $row['fourth'];    
}

while ($row['positions'] == "fifth" || "sixth")
{
    echo "$row['fifth']<br>";
    echo $row['sixth'];
}

我希望它可以检查行是否包含字符串,然后返回true,但是仅显示最后一个带有fifthsixth的字符串。有更好的方法吗?

0 个答案:

没有答案