2016-01-07 119 views
0

我得到了一個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'; 
+0

你可以做'$ cfgrang [ 'db_range'] [ 'db_texts'] [ 'DB_NAME']'和類似的東西來訪問每個值。 –

+0

有沒有提供這些信息的訪問方式? –

+0

這將無法正常工作,對此感到抱歉...如果您想使用索引訪問array_keys,請改爲 –

回答

0

您可以創建一個新的數組作爲

$db_credentials = $cfgrang['db_range']['db_texts'][]; 

,那麼你可以很容易地通過$db_credentials ['dbname'], $db_credentials['username'];

+0

好像** Nijraj **在輸入答案時說了同樣的話。 –

+0

嗨,兄弟,謝謝。請檢查主帖 –

0

如果您要訪問您的數組作爲

$cfgrange[0][0][0]; 

除了

$cfgrange["db_range"]["db_texts"]["dbname"]; 

比你的陣列獲取的用戶名,則dbname等應聲明爲

$cfgrange = array(
"0" => array(
     "0" => array(
      "0" => "DBNAME", 
      "1" => "", 
      "2" => "", 
      "3" => "" 
     )), 
     "db_range" => array(
     "db_texts" => array(
    "dbname" => "DBNAME", 
    "username" => "", 
    "password" => "", 
    "host" => "" 
    )), 

    ); 

現在你可以爲你的需要訪問

echo $cfgrange["db_range"]["db_texts"]["dbname"]; 
echo "<BR>".$cfgrange[0][0][0];