Using require files in functions

Discussion in 'PHP' started by arp059, Sep 19, 2009.

  1. #1
    Hi,

    I am working on a php project which users multiple functions. All functions are written in a single file called functions.php. Assume I am calling one such function from index.php.

    Now, let me describe my problem:

    The function I am calling expects a database connection.
    I set my database connection in another file called dbconn.php and include that in both index.php and functions.php
    But, even after including (I am using require_once() function) the db connection file in both index.php and functions.php, I am not able to use the same in function.

    Even if I insert a require_once inside the function definition, it did not work for me.

    Could anyone please help in getting the global db connection configuration which can make it easy to modify if I move servers - otherwise, it is going to be too difficult to handle during server migration.

    Thanks in advance :)
     
    arp059, Sep 19, 2009 IP
  2. Oli3L

    Oli3L Active Member

    Messages:
    207
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    70
    #2
    inside the function write:

    global $mysql_var;

    that should make it work.
     
    Oli3L, Sep 19, 2009 IP
  3. arp059

    arp059 Peon

    Messages:
    65
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    @Oil3L

    Thanks for that. Could you please elaborate more on that with example.

    Google does not have enough details for that.

    what should I write in dbconn.php, functions.php and index.php

    Thanks :)
     
    arp059, Sep 19, 2009 IP
  4. Oli3L

    Oli3L Active Member

    Messages:
    207
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    70
    #4
    Ok let's say that inside dbconn.php you have this:

    
    
    $conn = mysql_connect($host,$username,$password);
    mysql_select_db($db_database,$conn);
    
    
    PHP:
    And after you include it, use this in a function:

    
    
    function tFunc($var) {
    global $conn;
    
    /*
    + Rest of the code
    */
    
    }
    
    
    
    PHP:

    That should resolve your problem.
     
    Oli3L, Sep 19, 2009 IP
  5. arp059

    arp059 Peon

    Messages:
    65
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    wow man, that's nice..

    It worked.

    Thanks a lot :) God bless :)
     
    arp059, Sep 19, 2009 IP
  6. Oli3L

    Oli3L Active Member

    Messages:
    207
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    70
    #6
    I'm glad I could help :)
     
    Oli3L, Sep 19, 2009 IP
  7. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Another way would be to pass the connection in the function parameters, e.g.

    function tFunc($var, $conn) {
    /*
    + Rest of the code
    */
    
    }
    Code (markup):
     
    szalinski, Sep 19, 2009 IP