Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP数组配置文件_Php_Mysql - Fatal编程技术网

PHP数组配置文件

PHP数组配置文件,php,mysql,Php,Mysql,正如你可能会告诉我的,我是新手,并试图通过创建一个包含我网站所有设置的配置文件,让我的生活更轻松一些 我这样做是因为我在学习,并试图把我的知识更努力一点,一遍又一遍地重复相同的代码 我创建了一个名为settings.php的文件,目前的代码如下 <?php return array( 'host' => 'localhost', 'username' => 'me', 'password' => 'password', 'db_name'

正如你可能会告诉我的,我是新手,并试图通过创建一个包含我网站所有设置的配置文件,让我的生活更轻松一些

我这样做是因为我在学习,并试图把我的知识更努力一点,一遍又一遍地重复相同的代码

我创建了一个名为settings.php的文件,目前的代码如下

<?php

return array(
    'host' => 'localhost',
    'username' => 'me',
    'password' => 'password',
    'db_name' => 'mydb'
);

?>

在我的页面中,我输入了以下代码

<?php
//set connection variables
$host = include('settings.php');
$username = include('settings.php');
$password = include('settings.php');
$db_name = include('settings.php');

//connect to mysql server
$mysqli = new mysqli($host, $username, $password, $db_name);

//check if any connection error was encountered
if(mysqli_connect_errno()) {
    echo "Error: Could not connect to database.";
    exit;
}
?>

我从另一个可以找到的问题中得到了这个代码

这可能不起作用,但我不能确定,因为它没有给出错误


在继续之前,我想添加将存储在数据库中的其他设置。这是我执行此操作的最佳方式吗?

以下是您使用代码的方式

<?php
//set connection variables
$config = include('settings.php');

//connect to mysql server
$mysqli = new mysqli($config['host'], $config['username'], $config['password'], $config['db_name']);

//check if any connection error was encountered
if(mysqli_connect_errno()) {
    echo "Error: Could not connect to database.";
    exit;
}
?>

以下是如何使用代码

<?php
//set connection variables
$config = include('settings.php');

//connect to mysql server
$mysqli = new mysqli($config['host'], $config['username'], $config['password'], $config['db_name']);

//check if any connection error was encountered
if(mysqli_connect_errno()) {
    echo "Error: Could not connect to database.";
    exit;
}
?>

以下是如何使用代码

<?php
//set connection variables
$config = include('settings.php');

//connect to mysql server
$mysqli = new mysqli($config['host'], $config['username'], $config['password'], $config['db_name']);

//check if any connection error was encountered
if(mysqli_connect_errno()) {
    echo "Error: Could not connect to database.";
    exit;
}
?>

以下是如何使用代码

<?php
//set connection variables
$config = include('settings.php');

//connect to mysql server
$mysqli = new mysqli($config['host'], $config['username'], $config['password'], $config['db_name']);

//check if any connection error was encountered
if(mysqli_connect_errno()) {
    echo "Error: Could not connect to database.";
    exit;
}
?>

它不会给出错误,因为这都是有效的代码,但它没有按照您的想法执行。通常在页面顶部包含一次脚本,然后该脚本中定义的任何变量都可以在执行包含操作的页面中使用

includeMe.php

<?php
$settings = array(
    'host' => 'localhost',
    'username' => 'me',
    'password' => 'password',
    'db_name' => 'mydb'
);

它不会给出错误,因为这都是有效的代码,但它没有按照您的想法执行。通常在页面顶部包含一次脚本,然后该脚本中定义的任何变量都可以在执行包含操作的页面中使用

includeMe.php

<?php
$settings = array(
    'host' => 'localhost',
    'username' => 'me',
    'password' => 'password',
    'db_name' => 'mydb'
);

它不会给出错误,因为这都是有效的代码,但它没有按照您的想法执行。通常在页面顶部包含一次脚本,然后该脚本中定义的任何变量都可以在执行包含操作的页面中使用

includeMe.php

<?php
$settings = array(
    'host' => 'localhost',
    'username' => 'me',
    'password' => 'password',
    'db_name' => 'mydb'
);

它不会给出错误,因为这都是有效的代码,但它没有按照您的想法执行。通常在页面顶部包含一次脚本,然后该脚本中定义的任何变量都可以在执行包含操作的页面中使用

includeMe.php

<?php
$settings = array(
    'host' => 'localhost',
    'username' => 'me',
    'password' => 'password',
    'db_name' => 'mydb'
);

使用您的方法有几种方法,但这里有一种:

$settings = include('settings.php');
$username = $settings['username'];
//etc..
但是,放弃退货业务,这样做:

$settings = array(
    'host' => 'localhost',
    'username' => 'me',
    'password' => 'password',
    'db_name' => 'mydb'
);
使用“包括”菜单中的
$settings

include('settings.php');
$username = $settings['username'];
//etc..
或者我会在需要的地方使用
$settings['username']
,而不是将其分配给
$username

$mysqli = new mysqli($settings['host'], $settings['username'], $settings['password'], $settings['db_name']);

使用您的方法有几种方法,但这里有一种:

$settings = include('settings.php');
$username = $settings['username'];
//etc..
但是,放弃退货业务,这样做:

$settings = array(
    'host' => 'localhost',
    'username' => 'me',
    'password' => 'password',
    'db_name' => 'mydb'
);
使用“包括”菜单中的
$settings

include('settings.php');
$username = $settings['username'];
//etc..
或者我会在需要的地方使用
$settings['username']
,而不是将其分配给
$username

$mysqli = new mysqli($settings['host'], $settings['username'], $settings['password'], $settings['db_name']);

使用您的方法有几种方法,但这里有一种:

$settings = include('settings.php');
$username = $settings['username'];
//etc..
但是,放弃退货业务,这样做:

$settings = array(
    'host' => 'localhost',
    'username' => 'me',
    'password' => 'password',
    'db_name' => 'mydb'
);
使用“包括”菜单中的
$settings

include('settings.php');
$username = $settings['username'];
//etc..
或者我会在需要的地方使用
$settings['username']
,而不是将其分配给
$username

$mysqli = new mysqli($settings['host'], $settings['username'], $settings['password'], $settings['db_name']);

使用您的方法有几种方法,但这里有一种:

$settings = include('settings.php');
$username = $settings['username'];
//etc..
但是,放弃退货业务,这样做:

$settings = array(
    'host' => 'localhost',
    'username' => 'me',
    'password' => 'password',
    'db_name' => 'mydb'
);
使用“包括”菜单中的
$settings

include('settings.php');
$username = $settings['username'];
//etc..
或者我会在需要的地方使用
$settings['username']
,而不是将其分配给
$username

$mysqli = new mysqli($settings['host'], $settings['username'], $settings['password'], $settings['db_name']);


我认为在你提到的问题中,设置被放在一个变量中(作为一个数组)。你这样做是不起作用的,因为你把数组设置成了你所有的变量。我认为在问题中你提到的设置被放在一个变量中(作为数组)。你这样做是不起作用的,因为你把数组设置成了你所有的变量。我认为在问题中你提到的设置被放在一个变量中(作为数组)。你这样做是不起作用的,因为你把数组设置成了你所有的变量。我认为在问题中你提到的设置被放在一个变量中(作为数组)。你这样做是行不通的,因为你将数组设置为所有变量。哦,不,放弃全局变量吧-1
$settings
仅当文件包含在全局范围内时才是全局的。还有,“使用你的方法”,但我同意。发布你的解决方案,我假设一个静态类?哦,不,放弃全局变量-1
$settings
仅当文件包含在全局范围内时才是全局的。还有,“使用你的方法”,但我同意。发布你的解决方案,我假设一个静态类?哦,不,放弃全局变量-1
$settings
仅当文件包含在全局范围内时才是全局的。还有,“使用你的方法”,但我同意。发布你的解决方案,我假设一个静态类?哦,不,放弃全局变量-1
$settings
仅当文件包含在全局范围内时才是全局的。还有,“使用你的方法”,但我同意。发布您的解决方案后,我假设一个静态类?相同:全局变量是邪恶的@奥列格没有投票否决所有人,只是说“全球都是邪恶的”,你为什么不提供一个更好的解决方案呢。必须有某种全局方式来访问变量。至少它们是在数组下命名的。因为@Christopher已经给出了正确的答案:那么你在“数组下命名”下的意思是什么@Oleg真的认为只有一个全局变量。名称空间在这里是个不好的词。我最近做了太多javascript。同样的:全局变量是邪恶的@奥列格没有投票否决所有人,只是说“全球都是邪恶的”,你为什么不提供一个更好的解决方案呢。必须有某种全局方式来访问变量。至少它们是在数组下命名的。因为@Christopher已经给出了正确的答案:那么你在“数组下命名”下的意思是什么@Oleg真的认为只有一个全局变量。名称空间在这里是个不好的词。我最近做了太多javascript。同样的:全局变量是邪恶的@Oleg代替