PHP-MYSQL致命错误:常量表达式包含无效操作

时间:2019-12-30 03:46:35

标签: php mysql

在尝试加载我的网站时,我的php脚本出现致命错误:

<?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;
    }

}

?>

它告诉我致命错误:第6行的D:\ Servers \ wamp64 \ www \ classes \ PDO.class.php中,常量表达式包含无效操作

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

用-> private static $dsn1 = 'mysql:host='+$_ENV['DB_HOST'];替换此-> private static $dsn1 = 'mysql:host='.$_ENV['DB_HOST'];

您不能像+那样使用JavaScript来组合字符串和变量,而必须使用.