gpt4 book ai didi

PHP - MYSQL fatal error : Constant expression contains invalid operations

转载 作者:行者123 更新时间:2023-11-29 15:18:45 30 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





PHP Error : Fatal error: Constant expression contains invalid operations

(5 个回答)


2年前关闭。




尝试加载我的网站时,我的 php 脚本出现 fatal error ,代码如下:

<?php

class PDO_DB {

public $dbh;
private static $dsn1 = 'mysql:host='+$_ENV['DB_HOST'];
private static $dsn2 = ';port=3306;dbname='+$_ENV['DB_NAME'];
private static $user = $_ENV['DB_USER'];
private static $pass = $_ENV['DB_PASS'];
private static $dbOptions = array(
//PDO::ATTR_PERSISTENT => true,
PDO::ATTR_CASE => PDO::CASE_LOWER,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION //TODO: remove this line on production (maybe not, just hide php errors, so I can see logs)
);

public static function factory() {

//$sock = '/var/run/mysql/mysql.sock'; //localhost
// $sock = '/var/lib/mysql/mysql.sock';

if(!isset(self::$dbh)){
$dbh = new PDO(self::$dsn1.self::$dsn2,self::$user,self::$pass, self::$dbOptions);
}
return $dbh;
}

}

?>

它告诉我 fatal error :常量表达式在第 6 行的 D:\Servers\wamp64\www\classes\PDO.class.php 中包含无效操作

谁能帮我 ?

最佳答案

替换这个-> private static $dsn1 = 'mysql:host='+$_ENV['DB_HOST']; , 用这个 -> private static $dsn1 = 'mysql:host='.$_ENV['DB_HOST'];
您不能使用 +组合字符串和变量,如 JavaScript ,您必须使用 .

关于PHP - MYSQL fatal error : Constant expression contains invalid operations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59525493/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com