如果其他条件“其他”不起作用


if else condition 'else' is not working

/MY CODE/

if 部分工作正常,但其他部分不起作用。我什至尝试了$variable而不是直接回声,但它仍然无法正常工作"否则"

更新

    <?php
   $db = new mysqli('localhost', 'root' ,'', 'timeline');
    if(!$db) {
        echo 'Could not connect to the database.';
    } else {
        if(isset($_POST['queryString'])) {
            $queryString = $db->real_escape_string($_POST['queryString']);
            if(strlen($queryString) >0) {
                $query = $db->query("SELECT collegename FROM college WHERE collegename LIKE '$queryString%' LIMIT 10");
                if(isset($query)) {
                echo '<ul>';
                    while ($result = $query ->fetch_object()) {
                        echo '<li onClick="fill('''.addslashes($result->collegename).''');">'.$result->collegename.'</li>';
                    }
                echo '</ul>';
                } else {
                    echo 'create some'; // this part is not working

                }
            } else {
                // do nothing
            }
        } else {
            echo 'There should be no direct access to this script!';
        }
    }
?>

帮我..... 甚至在堆栈溢出上阅读了很多类似的问题,但没有真正的回报

如果您使用的是 mysqli::query,那么您的 if(isset($query)) 语句将始终被评估为 true,因为$query要么是 FALSE 对象,要么是mysqli_result对象。 isset返回这两个值的TRUE,因此永远不会调用else代码。

关于 isset 的文档:

如果 var 存在且值不是 NULL,则返回 TRUE,否则返回 FALSE。

请改用if($query !== false)

更新

您似乎也在检查$query以查看数据库中是否有命中。您需要为此检查结果中的行数,例如:

if ($query !== false && $query->num_rows > 0) {
   // Query was ok and at least one row was returned
}
else {
   // Will be reached if query was bad or there were no hits
}

试试

    if($query_run = $db->query("SELECT collegename FROM college WHERE collegename LIKE '$queryString%' LIMIT 10")){
    echo '<ul>';
                  while ($result = $query ->fetch_object()) {
                            echo '<li onClick="fill('''.addslashes($result->collegename).''');">'.$result->collegename.'</li>';
                  }
 echo '</ul>';
    } else {
         echo 'create some';
    }