php - $ 符号的 parse_ini_file 问题

标签 php database connection

我有一个包含我的数据库详细信息的 settings.ini.php 文件。

;<?php return; ?>
[SQL]
host = localhost
user = root
password =Gohead123$
dbname = nri_demo

**db.class.php**
private function Connect()
        {
            $this->settings = parse_ini_file("settings.ini.php");
                     echo '<pre>';
                     print_r($this->settings );exit;
            $dsn = 'mysql:dbname='.$this->settings["dbname"].';host='.$this->settings["host"].'';
            try 
            {
                # Read settings from INI file, set UTF8
                $this->pdo = new PDO($dsn, $this->settings["user"], $this->settings["password"], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

                # We can now log any exceptions on Fatal error. 
                $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                # Disable emulation of prepared statements, use REAL prepared statements instead.
                $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

                # Connection succeeded, set the boolean to true.
                $this->bConnected = true;
            }
            catch (PDOException $e) 
            {
                # Write into log
                echo $this->ExceptionLog($e->getMessage());
                die();
            }
        }

如果我用密码解析配置(settings.ini.php)文件,则不会显示任何内容。如果我像这样更改密码

[SQL]
host = localhost
user = root
password =Gohead123
dbname = nri_demo

我得到这样的输出

Array
(
    [host] => localhost
    [user] => root
    [password] => Gohead123
    [dbname] => nri_demo
)

我该如何解决这个问题?请帮助我。

最佳答案

尝试使用 parse_ini_file 的第三个参数作为 INI_SCANNER_RAW

parse_ini_file('settings.ini.php', false, INI_SCANNER_RAW)

输出:

Array ( [host] => localhost [user] => root [password] => Gohead123$ [dbname] => nri_demo )

关于php - $ 符号的 parse_ini_file 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42483718/

相关文章:

mysql - 数据库设计以获得尽可能多的统计数据

Java 套接字服务器 : How to handle many incoming connections?

c# - 如何使用 C# 通过服务器站点的 TCP/IP 连接到 SQL 服务器?

php - 比较给定日期和今天

php - 用于搜索大写字母/单词的正则表达式?

java - 保持多对多关系时出现 PropertyAccessException

python - 更新 Django 数据库 - 脚本

ios - 是什么导致 iOS 游戏被添加到用户的游戏连接中?

php - LDAP - 从 DN 中提取 CN 值

php - json 文件格式错误 - 如何找出位置?