php链接本地mysql数据库配置文件_PHP读取配置文件连接MySQL数据库

读取配置文件方法parse_ini_file($filepath [,$section])

代码:

conn.php

//连接数据库

//$conn =new mysqli('localhost','root','','test') or die("连接失败
");

//读取配置文件

$ini= parse_ini_file("test.ini");

$conn =new mysqli($ini["servername"],$ini["username"],$ini["password"],$ini["dbname"]) or die("连接失败
");

//操作数据库

$result=$conn->query("select * from cartoon;");

//输出数据

while($row=$result->fetch_assoc()){

print_r($row);

echo "
";

}

//关闭数据库

$conn->close();

?>

test.ini

[mysql]

servername="localhost"

username="root"

password=""

dbname="test"

输出

f1f9d1b2582583720e4e25ac92811fa7.png

1、parse_ini_file() 函数解析一个配置文件,并以数组的形式返回其中的设置。

语法:

parse_ini_file(file,process_sections)

124679662f32aad3bf4cfe4c0ddae6d3.png

2.例子1:

"test.ini" 的内容:

48304ba5e6f9fe08f3fa1abda7d326ab.png

[names]

me = Robert

you = Peter

[urls]

first = "http://www.example.com"

second = "http://www.w3school.com.cn"

48304ba5e6f9fe08f3fa1abda7d326ab.png

PHP 代码:

$tmp = parse_ini_file("test.ini");

var_dump($tmp);

?>

输出:

48304ba5e6f9fe08f3fa1abda7d326ab.png

array(4) {

["me"]=>

string(6) "Robert"

["you"]=>

string(5) "Peter"

["first"]=>

string(22) "http://www.example.com"

["second"]=>

string(26) "http://www.w3school.com.cn"

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

例子2:

"test.ini" 的内容:

48304ba5e6f9fe08f3fa1abda7d326ab.png

[names]

me = Robert

you = Peter

[urls]

first = "http://www.example.com"

second = "http://www.w3school.com.cn"

48304ba5e6f9fe08f3fa1abda7d326ab.png

PHP 代码(process_sections 设置为 true):

$tmp = parse_ini_file("test.ini",true);

var_dump($tmp);

?>

输出:

48304ba5e6f9fe08f3fa1abda7d326ab.png

array(2) {

["names"]=>

array(2) {

["me"]=>

string(6) "Robert"

["you"]=>

string(5) "Peter"

}

["urls"]=>

array(2) {

["first"]=>

string(22) "http://www.example.com"

["second"]=>

string(26) "http://www.w3school.com.cn"

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值