SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2001
    Location
    Atlanta
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to declare global

    Hi everyone,

    I have a database connection file database.php3, which has different functions. Like Add, Delete, Update etc.

    And I'm including the database information file database_info.php3 as a global but it dosen't work for different functions, I have to respectively call the database_info.php3 file in every single function as local to that function.

    How I can call that file globaly, and if I declare that file local to the function could it effect the speed or the efficiency of program.

    Thank you
    Aneal

  2. #2
    What's HTML?
    Join Date
    May 2001
    Location
    San Diego, CA
    Posts
    1,701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't need to include your database file in each function, but you do need to declare the variables global in each function.

    For example:
    //database.php
    $cnx = "localhost";
    $name = "bobjones";
    $pass = "imrich";
    $database = "database1";

    //index.php
    include("database.php");

    function connectDB()
    {
    // This is what you need
    global $cnx, $name, $pass, $database;

    // Everything else...
    }
    Ryan Kuhle - A Proud Advisor - Got Questions? Just Ask!
    Get your website started for less than $20! Click Here

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2001
    Location
    Atlanta
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks

    It is great. It is alot better than
    require ("database.php");
    in every function.
    Aneal

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Even easier would be to assign your mysql_connection to a variable outside the function in some global page then just globalize the $link id like

    database.php
    PHP Code:
    $cnx "localhost"
    $name "bobjones"
    $pass "imrich"
    $database "database1"
    $db mysql_connect($cnx$name$pass);
    mysql_select_db($database); 

    In your script
    PHP Code:
    include("database.php");

    function 
    foo() {
    global 
    $db;

    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    What's HTML?
    Join Date
    May 2001
    Location
    San Diego, CA
    Posts
    1,701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I always connect to MySQL and select the database at the beginning of the script instead of the individual functions. If each operation of the script requires database access, why waste space calling the same operation several times?
    Ryan Kuhle - A Proud Advisor - Got Questions? Just Ask!
    Get your website started for less than $20! Click Here

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ryan, you are merely re-enforcing what I just said. Was that your intention, or did you not understand my post. I am saying exactly what you are, make your db connection outside the functions at the start of your script. Then simply pass the link id into the function.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  7. #7
    What's HTML?
    Join Date
    May 2001
    Location
    San Diego, CA
    Posts
    1,701
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh geez, sorry Louie. Yeh, I misunderstood what you said. Didn't mean to repeat...
    Ryan Kuhle - A Proud Advisor - Got Questions? Just Ask!
    Get your website started for less than $20! Click Here

  8. #8
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No worries, didn't mean to sound like an a*s, I figured you probably just misunderstood my post.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •