doukezi4576 2016-01-28 15:10
浏览 16
已采纳

INI文件中的数据库信息[重复]

This question already has an answer here:

For my school project I need to recover some information one database and associate with another database. But for that I want to use a ini file because if the log for connection of one data base change, I don't want to change it in the code.

My code is :

<?php
// On recupére les info dans fichier ini pour mySQL
//Get Information in ini for mySQL

$fichier = 'BDDconnexion.ini';

if(file_exists($fichier)){

$config = parse_ini_file($fichier,true);

$ip = "$config['mySQL'][ip]";
$port = "$config['mySQL'][port]";
$nomBDD = "config['mySQL'][nomBDD]";
$login = "$config['mySQL'][login]";
$password = "$config['mySQL'][password]";


}

// On se connecte à MySQL
//Connexion to MySQL
try {
$bdd = new PDO(mysql . ':host='.$ip.'dbname='.$nomBDD,$login,$password,array(PDO::ATTR_ERRMODE =>  PDO::ERRMODE_EXCEPTION));
    } 
catch (Exception $e) 
    {
     die('Erreur : '. $e->getMessage());
    }


?>

It is not working and i have this error message :

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\connectmySQL.php on line 14.

And the composition of my ini file is :

[mySQL]
ip="127.0.0.1"
port=4900
nomBDD=MagicCash
login="******"
password=""

Can someone help me ?

</div>
  • 写回答

1条回答 默认 最新

  • drnf09037160 2016-01-28 15:13
    关注

    Basic PHP: You cannot have quoted array keys in a "-quoted string:

    $ip = "$config['mySQL'][ip]";
                   ^-----^--- incorrect
    

    The following are proper syntax for such things:

    $ip = "foo $arr[key] bar";
    $ip = "foo {$arr['key']} bar"; // note the {}
    $Ip = "foo " . $arr['key'] . " bar";
    

    Also note that you're using a multidimensional array, which will also cause problems. PHP's parser is not greedy:

    $foo['bar']['baz'] = 'qux';
    echo "$foo[bar][baz]"; // output is "Array[baz]"
    

    For multi-dimensional array variables in a "-quoted string, you MUST use the {} notation:

    echo "{$foo['bar']['baz']}"; // outputs 'qux'
    

    And of course, in the greater scheme, your variables don't need to be quoted AT ALL. These two statements

    $foo = "$bar";
    $foo = $bar;
    

    are essentially functionally identical, except the "$bar" version forces PHP to create a new string, fill in the $bar value, then assign it to $foo. Sometimes this is desired if $bar is something OTHER than a string, but generally it's a waste of cpu cycles.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端