title: Setting up a db connection using variables from another file
body: <p>I am attempting to set up a db connection for a mysqli_connect statement $dbc. I had it working in my script with the following:</p>
  
  <pre><code>DEFINE ('DB_USER','myName';
  DEFINE ('DB_PASSWORD','somePass123';
  DEFINE ('DB_HOST','localhost';
  DEFINE ('DB_NAME','sitename';
  
  
  // make the db connection
      $dbc = @mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME)
      OR die ('Could not connect to mysql: ' . mysqli_connect_error());
  </code></pre>
  
  <p>I then posted a question on SO about security and best practice and was advised that if the connection variables (password, host, dbname, user) exist elsewhere in a separate database configuration file, that I should call the variables for the connection from there.</p>
  
  <p>Here is the relevant snippet from the file in config/database/php</p>
  
  <pre><code>$config['default'] = array(
      'benchmark' =&gt; TRUE,
      'persistent' =&gt; FALSE,
      'connection' =&gt; array(
          'type' =&gt; 'mysqli',
          'user' =&gt; 'myName',
          'pass' =&gt; 'somepass123',
          'host' =&gt; 'localhost',
          'port' =&gt; FALSE,
          'socket' =&gt; FALSE,
          'database' =&gt; 'sitename',
      ),
      'character_set' =&gt; 'utf8',
      'table_prefix' =&gt; '',
      'object' =&gt; TRUE,
      'cache' =&gt; FALSE,
      'escape' =&gt; TRUE
  );
  </code></pre>
  
  <p>So I then tried this: </p>
  
  <pre><code>DEFINE ('DB_USER','$config['default']['connection']['user'])';
  DEFINE ('DB_PASSWORD',$config['default']['connection']['pass']);
  DEFINE ('DB_HOST',$config['default']['connection']['host']);
  DEFINE ('DB_NAME',$config['default']['connection']['database']);
  
  
  // make the db connection
      $dbc = @mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME)
      OR die ('Could not connect to mysql: ' . mysqli_connect_error());
  </code></pre>
  
  <p>Which produced the following error when I tried to load the page:
  "Undefined variable: config"</p>
  
  <p>I then tried this:</p>
  
  <pre><code> $dbc = @mysqli_connect(
      $config['default']['connection']['host'],
      $config['default']['connection']['user'],
      $config['default']['connection']['pass'],
      $config['default']['connection']['database'])
  
      OR die ('Could not connect to mysql: ' . mysqli_connect_error());
  
  // Set the encoding
  mysqli_set_charset($dbc, 'utf8');
  
  // set the query variable
  $query = "SELECT MAX(rating_date) 
            AS last_date
            FROM rating
            WHERE incident_id = $incident_id;";
  
  //connect and run the query
  $result = mysqli_query($dbc, $query);
  
   echo $result-&gt;fetch_object()-&gt;last_date;
  
  ?&gt;
  </code></pre>
  
  <p>Which then produced the following error:
  "Fatal error: Call to a member function fetch_object() on a non-object in..."</p>
  
  <p>I'm not 100% sure what this is telling me. Yesterday I learned that one cannot echo a SQL query in php directly because it's an object so I cut n pasted the part "echo $result->fetch_object()->last_date;" which worked.</p>
  
  <p>It seems that now that I am trying to draw upon the variables from config, rather than just define them in the function, I am unable to connect perhaps due to a scope issue?</p>
  
  <p>The file with the $dbc connection is in a folder themes/myname/views/reports/detail.php</p>
  
  <p>The file with the database configuration details array is in application/config/database.php</p>
  
  <p>It looks like an issue of variable scope?</p>
  
  <p>What would be the best practice here in making my $dbc variable? How do I call the variables from database.php when it exists in a different directory than the file where I'm calling it? Must I include() the whole file?</p>