简体   繁体   English

数据库逻辑问题的动态切换

[英]Dynamic Switch from Database Logic Question

While looking through stackoverflow I found the following: How to create a dynamic PHP switch statement from MySQL query在查看 stackoverflow 时,我发现了以下内容: How to create a dynamic PHP switch statement from MySQL query

Which resulted in me trying something I haven't before, it doesn't work, but I am curious as to why it doesn't work rather than trying to figure out a way to make it work.这导致我尝试了一些我以前没有尝试过的东西,它不起作用,但我很好奇为什么它不起作用而不是试图找出一种方法让它起作用。

Here's my code:这是我的代码:

function SOMENAME(){
global $mysqli ;
$out = false ;
$VALIDTOKENS = array();
$sql = "SELECT * FROM `tokens` ORDER BY `tokens`.`id` ASC";
            if($result = mysqli_query($mysqli, $sql)) {
                while ($row = mysqli_fetch_assoc($result)) {
                    $VALIDTOKENS[$row['token']] = $row['token'];
                    }
            }
    if(isset($_GET['key'])) {
    $key = $_GET['key'];    
    }

    switch($key) {
            case $VALIDTOKENS[$key]:
                $out = true ;
            break;
    } 
return $out ;
}

The function always returns false. function 始终返回 false。

From the logic of your code,when it returns false, there are many possibilities, such as the database failed to link, the query failed, the database table does not exist, the field does not exist, etc. You are advised to debug with breakpoints, you can use var_dump() in different lines of code, check the returned results.从你的代码逻辑来看,返回false的时候,有很多种可能,比如链接数据库失败,查询失败,数据库表不存在,字段不存在等等,建议你调试断点,可以在不同的代码行中使用var_dump(),查看返回的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM