Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

PHP variables, includes problem

  • 04-07-2006 7:45pm
    #1
    Registered Users, Registered Users 2 Posts: 1,086 ✭✭✭


    I am building a site using PHP.

    It has many pages, so to make some sort of consistency I made myself a config.php.

    This contains
    //config.php
    
    	$config = array();
    
            $config['db_user'] = "username";
    	
    	$config['db_password'] = "password";
    	
    	$config['db_name'] = "database_name";
    
            $config['results_per_page'] = "database_name";
    
    //etc....
    

    Instead of including this file to each page, I included it (using include "config.php") in a file called commonly_used_methods.php which is already included in each page (using include "commonly_used_methods.php").
    //commonly_used_methods.php
    
    function add($r,$y)
    { 
              return ($r+$y);
    }
    
    function conn()
    {
              $dbh=mysql_connect ("localhost", $config['db_user'], $config['db_password']) or die ('I cannot connect to the database because: ' . mysql_error());
      		mysql_select_db ($config['db_name']);
    }
    
    include "config.php"; 
    
    //etc....
    


    For some reason when I echo values on the webpage, I can use the values from the config.php file without a problem. However when I try to run methods containing the variables from config.php, such as conn() they appear to be empty?

    I does not matter if I include config.php at the start or at the end of commonly_used_methods.php

    Can anyone help?

    Thanks

    Peter


Comments

  • Registered Users, Registered Users 2 Posts: 6,414 ✭✭✭kdouglas


    try declaring the $config variable as:
    [PHP]
    global $config=array();
    [/PHP]
    and then in each method where you want to use it, declare:

    [PHP]global $config;[/PHP]


  • Technology & Internet Moderators Posts: 28,830 Mod ✭✭✭✭oscarBravo


    Are you assigning the $config array in a function?


  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    You need to have the
    include("config.php")
    at the top of the other page. You will also need to declare the config array as global inside each function which uses it. Just "global $config" at the start of each function.


  • Registered Users, Registered Users 2 Posts: 1,086 ✭✭✭Peter B


    Cheers Guys,

    Problem solved!

    Just shows how little I know about variable storage in memory for PHP programs.


Advertisement