#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    54
    Rep Power
    1

    Includes / Require Not working?


    I have a config.php which connects to the the HOST / DB

    when i include it into another file it does not carry over.

    EXAMPLE:
    In the Functions.php File, I require_once config.php. However, when i go to query something i receive the "undefined variable $con." However, the $con is defined in the config.php file.

    What am i doing wrong?!


    FIXED: I needed GLOBAL. lol
    Last edited by ide; August 10th, 2013 at 10:04 PM.
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    Globals are bad design and shouldn't be relied upon. You should use the singleton pattern for database connection resources or pass them all into the functions.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,868
    Rep Power
    368
    by globals do you mean:

    file1.php

    define('DB_NAME',"name");..

    file2.php

    include file1.php

    echo DB_Name?


    if these are bad then what is the alternative? What I do is have a folder with different db_conn definition. I then include them in the individual projects.. so i dont think they are "global" per se.. but want clarificatio
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    Those are constants, not globals. Constants are alright since they can only really exist in one place (and if your code is correct you'll have only one file full of constants).

    GLOBALS, in the sense of:
    PHP Code:
    $a "hello";

    function print() {
      global 
    $a;
      echo 
    $a;

    Those are bad. They break the entire concept of functions (and usually classes) and make your code an unreadable, messy, bug-laden tangle of randomly interrelated snippets of code, none of which are independent and none of which can be tested or reliably traced.

    The proper way to do what you're doing Paul is to use the singleton pattern to define a DB_Conn class, so regardless of where you are in the code you can do:
    $conn = DB_Conn::getInstance();

    The getInstance() function stores the authentication information for the database (or puts it as static methods on the class itself) and you can have something globally accessible without running into inheritance or dependency issues.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,868
    Rep Power
    368
    thanks for explaining
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    54
    Rep Power
    1
    Originally Posted by ManiacDan
    Those are constants, not globals. Constants are alright since they can only really exist in one place (and if your code is correct you'll have only one file full of constants).

    GLOBALS, in the sense of:
    PHP Code:
    $a "hello";

    function print() {
      global 
    $a;
      echo 
    $a;

    Those are bad. They break the entire concept of functions (and usually classes) and make your code an unreadable, messy, bug-laden tangle of randomly interrelated snippets of code, none of which are independent and none of which can be tested or reliably traced.

    The proper way to do what you're doing Paul is to use the singleton pattern to define a DB_Conn class, so regardless of where you are in the code you can do:
    $conn = DB_Conn::getInstance();

    The getInstance() function stores the authentication information for the database (or puts it as static methods on the class itself) and you can have something globally accessible without running into inheritance or dependency issues.
    WAIT, so using [code]global $connection;[code] is bad practice?
  12. #7
  13. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    Yes it is. For a database connection it's not horrible, but the "global" keyword in general is considered bad practice. Using it for database connections (and other resource types) is the least bad way to use it.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo