通过循环定义常量


Define constant through looping

我尝试通过循环定义常量值,但它得到错误:

Notice: Use of undefined constant XXXXX - assumed 'XXXXX' in XX on line XX

这是代码:

$q = "SELECT `Key`, `Value` FROM appconfig";
//I skip the line to read from database
while($fetch = mysqli_fetch_array($r)) {
    define("'" . $fetch["Key"] . "'", $fetch["Value"]);
    //I try to echo $fetch["Key"] and $fetch["Value"] and it returns value
}

代码有问题吗?我尝试手动定义它,它工作。

不要使用引号,你不需要它们:

define($fetch["Key"], $fetch["Value"]);