我得到了一個config.php,裏面有3個數據庫憑證。 問題是:我沒有得到確定的DB憑據。我不知道如何訪問內部陣列陣列,而不該數組內提供信息,我不能這樣做,因爲我想這一切只有在config.php
我的config.php:從PHP中獲取數組數組中的數據
<?php
//Do not change identificators
$cfgrange = array(
"db_range" => array(
"db_texts" => array(
"dbname" => "",
"username" => "",
"password" => "",
"host" => ""
),
"db_images" => array(
"dbname" => "",
"username" => "",
"password" => "",
"host" => ""
),
"db_users" => array(
"dbname" => "",
"username" => "",
"password" => "",
"host" => ""
)
),
"url_range" => array(
"baseUrl" => "",
"imagesUrl" => ""
),
"path_range" => array(
"resources" => $_SERVER["DOCUMENT_ROOT"] . "/resources",
"library" => $_SERVER["DOCUMENT_ROOT"] . "/resources/library",
"validators" => $_SERVER["DOCUMENT_ROOT"] . "/resources/validators",
"images" => array(
"content" => $_SERVER["DOCUMENT_ROOT"] . "/images/content",
"layout" => $_SERVER["DOCUMENT_ROOT"] . "/images/layout"
)
),
"email_range" => array(
"contact" => "http://example.com",
"admin" => "http://example.com"
)
);
/* ex. require_once(LIBRARY_PATH . "Paginator.php") */
defined("LIBRARY_PATH")
or define("LIBRARY_PATH", realpath(dirname(__FILE__) . '/library'));
/* Error reporting. */
ini_set("error_reporting", "true");
error_reporting(E_ALL|E_STRCT);
?>
我accountlogin.php
<?PHP
$selected_db_users = 'true';
include('../library/connection.php');
//rest of the code
connection.php
<?php
ob_start();
include('../config.php');
if ($selected_db_text == 'true'){
$dbname = $cfgrange[0][0][0];
$username = $cfgrange[0][0][1];
$password = $cfgrange[0][0][2];
$host = $cfgrange[0][0][3];
} elseif($selected_db_images == 'true'){
$dbname = $cfgrange[0][1][0];
$username = $cfgrange[0][1][1];
$password = $cfgrange[0][1][2];
$host = $cfgrange[0][1][3];
} elseif($selected_db_users == 'true'){
$dbname = $cfgrange[0][2][0];
$username = $cfgrange[0][2][1];
$password = $cfgrange[0][2][2];
$host = $cfgrange[0][2][3];
}
if(!isset($_SESSION)){
session_start();
}
$username = "";
$password = "";
$host = "";
$dbname = "";
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
try{
$db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
}
catch(PDOException $ex){
die("Failed to connect to the database: " . $ex->getMessage());
}
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()){
function undo_magic_quotes_gpc(&$array){
foreach($array as &$value)
{
if(is_array($value)){
undo_magic_quotes_gpc($value);
}else{
$value = stripslashes($value);
}
}
}
undo_magic_quotes_gpc($_POST);
undo_magic_quotes_gpc($_GET);
undo_magic_quotes_gpc($_COOKIE);
echo 'ok';
}
$selected_db_text = 'false';
$selected_db_images = 'false';
$selected_db_users = 'false';
你可以做'$ cfgrang [ 'db_range'] [ 'db_texts'] [ 'DB_NAME']'和類似的東西來訪問每個值。 –
有沒有提供這些信息的訪問方式? –
這將無法正常工作,對此感到抱歉...如果您想使用索引訪問array_keys,請改爲 –