PHP读取配置文件连接MySQL数据库

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 读取配置文件方法parse_ini_file($filepath [,$section])代码:conn.phptest.ini[mysql]servername="localhost"username="root"password=""dbname="test"输出  1、parse_ini_file() 函数解析一个配置文件,并以数组的形式返回其中的设置。
+关注继续查看

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

代码:

conn.php

<?php
	//连接数据库
	//$conn =new mysqli('localhost','root','','test') or die("连接失败<br/>");
	//读取配置文件
	$ini= parse_ini_file("test.ini");
	$conn =new mysqli($ini["servername"],$ini["username"],$ini["password"],$ini["dbname"]) or die("连接失败<br/>");

	
	//操作数据库
	$result=$conn->query("select * from cartoon;");
	
	//输出数据
	while($row=$result->fetch_assoc()){
		print_r($row);
		echo "<br/>";
	}
	
	//关闭数据库
	$conn->close();
?>

test.ini

[mysql]
servername="localhost"
username="root"
password=""
dbname="test"

输出

 

 

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

语法:

parse_ini_file(file,process_sections)

2.例子1:

"test.ini" 的内容:

复制代码
[names]
me = Robert
you = Peter

[urls]
first = "http://www.example.com"
second = "http://www.w3school.com.cn"
复制代码

PHP 代码:

<?php
  $tmp = parse_ini_file("test.ini");
  var_dump($tmp); ?>

输出:

复制代码
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"
}
复制代码

例子2:

"test.ini" 的内容:

复制代码
[names]
me = Robert
you = Peter

[urls]
first = "http://www.example.com"
second = "http://www.w3school.com.cn"
复制代码

PHP 代码(process_sections 设置为 true):

<?php
  $tmp = parse_ini_file("test.ini",true);
  var_dump($tmp); ?>

输出:

复制代码
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"
  }
}
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
3月前
|
关系型数据库 MySQL 数据库连接
PHP 原生连接 Mysql
PHP 原生连接 Mysql
72 0
|
3月前
|
关系型数据库 MySQL Unix
PHP MySql 安装与连接
PHP MySql 安装与连接
91 0
|
3月前
|
PHP
PHP 连接运算符
PHP 连接运算符
16 0
|
5月前
|
关系型数据库 MySQL PHP
PHP MySQL连接服务器
PHP MySQL连接服务器
|
8月前
|
SQL 网络协议 关系型数据库
PHP为什么可以连接MySQL?底层原理是什么?
PHP为什么可以连接MySQL?底层原理是什么?
234 0
|
12月前
|
PHP
记录一次php连接mysql8.0失败
记录一次php连接mysql8.0失败
110 0
|
SQL PHP 数据库
php连接sqlserver,php连接sql server数据库,php查询sqlserver数据库,php用sqlserver数据库
php连接sqlserver,php连接sql server数据库,php查询sqlserver数据库,php用sqlserver数据库
179 0
|
SQL 关系型数据库 MySQL
PHP与MySQL数据库——连接数据、插入数据、删除数据、返回结果集
本文章介绍了PHP连接MySQL数据库实现插入数据、删除数据、返回结果集的操作
267 0
PHP与MySQL数据库——连接数据、插入数据、删除数据、返回结果集
|
关系型数据库 MySQL PHP
PHP连接MySQL 8.0报错的解决办法
PHP连接MySQL 8.0报错的解决办法
289 0
|
NoSQL Linux PHP
【Redis】使用PHP连接远程Redis
【Redis】使用PHP连接远程Redis
505 0
【Redis】使用PHP连接远程Redis
推荐文章
更多