Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8197383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:43:48+00:00 2026-06-07T05:43:48+00:00

If I have a config.php file with variables in it like so… config.php :

  • 0

If I have a config.php file with variables in it like so…

config.php:

$cnf['dbhost'] = "0.0.0.0";
$cnf['dbuser'] = "mysqluser";
$cnf['dbpass'] = "mysqlpass";

How can I then access these variables from a class which is in another file, such as…

inc/db.class.php:

class db() {

  function connect() {
    mysql_connect($cnf['dbhost'], $cnf['dbuser'], $cnf['dbpass']);
  }

}
$db = new db();

So, I can use the class in another file such as…

index.php:

<html>
  <?php
    include('config.php');
    include('inc/db.class.php');
    $db->connect();
  ?>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:43:49+00:00Added an answer on June 7, 2026 at 5:43 am

    Include config file with include, require or require_once at the beginning of your db script. You will also need to specify $cnf as global in the function you want to use, otherwise you cannot access global variables:

    include "../config.php";
    
    class db() {
    
      function connect() {
          global $cnf;
          mysql_connect($cnf['dbhost'], $cnf['dbuser'], $cnf['dbpass']);
      }
    
    }
    $db = new db();
    

    EDIT:
    On big project I prefer to use a boot.php where I include all the php files, so I wont need to include in every file everything I need. Having this, I just need to include the boot into the index.php and have to disposition all the definitions. It’s slightly slower but really comfortable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a config.php file that creates an array, something like $config = array(
I have a php file that looks like this: <?php include(config.php); // put the
I have a file system structure with symlinks, something like this: folder123 index.php config.php
In PHP, i would have a config file that says something like $account =
I am using php and mysql. I have a Database config file (db-config.php) which
I have config.php with this... $dbhost = localhost; I want to be able to
I have a config.php file for one of my site which holds information needed
SOLVED: Error in template file I have Smarty setup like this: require_once 'smarty/Smarty.class.php'; $smarty
Here's my config file: <?php #config variables $host = ''; #your database host $user
I usually have a config-file with some global variables for database connectivity settings (such

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.