我的案例开关是什么都没有回报我

时间:2018-03-02 14:02:18

标签: php

我有这个案例开关,它是从sql数据库中获取数据,但不能解决我想要做的事情。我想输出案例的回报。看我的代码:

$link = mysqli_connect("localhost", "root", "", "table");
$q = mysqli_query($link, "SELECT * FROM sections WHERE id = " . $_GET['id'] . " ");
$id = $_GET['id'];

while ($row = mysqli_fetch_assoc($q)) {
    $sections = $row['section'];

    $sec = function($sections) {
        switch ($sections) {
            case "Solo":
                return "2:10"; // time limit
                break;
            case "Duo/Trio/Quartet":
                return "2:10"; // time limit
                break;
            case "Formation":
                return "4:20"; //time limit
                break;
            case "Groups":
                return "3:40"; //time limit
                break;
        }
    };

    $time_limit = $sec();
}

echo "Your moment has $time_limit seconds"; // example

1 个答案:

答案 0 :(得分:0)

我在这里没有使用匿名功能。您可以直接将time_limit分配给变量。

<?PHP
$link       = mysqli_connect("localhost", "root", "", "table");
$q          = mysqli_query($link, "SELECT * FROM sections WHERE id = " . mysqli_real_escape_string($link,$_GET['id'] . " "));
$time_limit = "";
while ($row = mysqli_fetch_assoc($q)) {
    $sections = $row['section'];
    switch ($sections) {
        case "Solo":
            $time_limit = "2:10"; //time limit
            break;
        case "Duo/Trio/Quartet":
            $time_limit = "2:10"; //time limit
            break;
        case "Formation":
            $time_limit = "4:20"; //time limit
            break;
        case "Groups":
            $time_limit = "3:40"; //time limit
            break;
    }
}
echo "Your moment has $time_limit seconds"; // example
?> 

另外:使用未经过计算的用户输入是一个非常糟糕的主意,因为您的代码容易受到SQL注入攻击。因此我添加了mysqli_real_escape_string