提问人:xlofev276 提问时间:3/28/2013 最后编辑:xlofev276 更新时间:3/28/2013 访问量:543
PHP 配置文件后未定义索引到数组中
PHP Undefined index after config file into array
问:
这是我的配置加载代码:
$WConfig;
$lines = file($ToRootDirectory . 'config.txt', FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line_num => $line) {
$line = trim($line);
if (!(substr($line, 0, 1) == '#')){
$WConfig[(trim(substr($line, 0, strpos($line, ":"))))] = trim(substr($line, strpos($line, ':') + 1));
}
}
unset($lines, $line, $line_num, $temp);
$host = $WConfig['mshost']; //line 11
print_r($WConfig); //line 12
它加载以下配置文件:(ANSI)
#--/ MySQL: //Dont forget to execute Install.sql ;)
# username: //NOT NEEDED TO BE ROOT -> Acces to INSERT, UPDATE, SELECT, SHOW
msusername:PHP_Default
# password:
mspassword:php
# database:
msdatabase:PHP_Default
# host:
mshost:localhost
#--/ Session:
# sessionend: Time in minutes when the session will be end from last acces. Default 20 minutes.
会话结束:20
但显示:
Notice: Undefined index: mshost in C:\######\PHP\LoadConfig.php on line 11
Array ( [msusername] => PHP_Default [mspassword] => php [msdatabase] => PHP_Default [mshost] => localhost [sessionend] => 20 )
第 11 行给出错误,因为他找不到“mshost”,但如果我在第 12 行显示数组,则“mshost”仍然存在。
谁知道这个问题的答案,我需要做些什么来解决这个问题?
更新:它仅由 msusername 和 mshost 显示
答:我已将 msusername 和 mshost 更改为数字 -> 0 和 1。这很好用。
答:
0赞
mishmash
3/28/2013
#1
您的配置文件看起来很像 INI。(实际上,与 INI 完全一样,除了 : 而不是 =)
我建议你使用PHP的INI加载例程来避免这种麻烦。
您的文件采用 INI 格式,如下所示:
; Use sections if you want
[MySQL]
;--/ MySQL: //Dont forget to execute Install.sql ;)
; username: //NOT NEEDED TO BE ROOT -> Acces to INSERT, UPDATE, SELECT, SHOW
msusername = PHP_Default
; password:
mspassword = php
; database:
msdatabase = PHP_Default
; host:
mshost = localhost
[Sessions]
;--/ Session:
; sessionend: Time in minutes when the session will be end from last acces. Default 20 minutes.
评论
0赞
xlofev276
3/28/2013
谢谢,我以前没有看过。但奇怪的是,数组中的变量找不到。
0赞
xlofev276
3/28/2013
我已经将代码和配置.txt更改为.ini标准,但他仍然找不到数组中出现的变量,但在使用 $WConfig[] 时找不到$WConfig = parse_ini_file($ToRootDirectory . 'config.ini');
;--/ MySQL: //Dont forget to execute Install.sql ;) ; username: //NOT NEEDED TO BE ROOT -> Acces to INSERT, UPDATE, SELECT, SHOW msusername=PHP_Default ; password: mspassword=php ; database:f msdatabase=loletje ; host: mshost=localhost ... sessionend=20
0赞
mishmash
3/28/2013
我编辑了我的帖子,给你一个例子,说明你的文件应该是什么样子。
0赞
mishmash
3/28/2013
哪些类型的错误?我在编辑我的帖子之前已经测试过它,它工作正常
0赞
xlofev276
3/28/2013
我已将 mshost 更改为数字 -> 0,现在我没有错误
评论