Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    CBG
    CBG is offline
    New Coder
    Join Date
    Feb 2010
    Location
    UK
    Posts
    91
    Thanks
    26
    Thanked 0 Times in 0 Posts

    Question Call db table from any PHP file

    Hi,

    I want to be able to call a database table that will be setup in another file called init.php, and be able to call it from any PHP file that has init.php included.

    Table is called abc_offline and has columns a,b,c in it.

    What I am wanting is to be able to call it from any PHP file by:
    $offline['a'] for example

    How do I do this, I been told I need an array in init.php but I don't know how to do this.

    Regards,
    CBG

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,473
    Thanks
    13
    Thanked 361 Times in 357 Posts
    just connect to the DB (given that init.php provides the necessary settings (like password, etc.)) using the PHP functions responsible for connecting to your DB type. but unless we know more about init.php, that is all I can currently say.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    CBG
    CBG is offline
    New Coder
    Join Date
    Feb 2010
    Location
    UK
    Posts
    91
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Hi,

    The DB connect is already setup.
    At the moment all that is in init.php is the connect for the DB and some calls to the database for other tables

    So I am looking for the code to calling the database and being able to call it for any file as $offline['a']

    Regards,
    CBG

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,473
    Thanks
    13
    Thanked 361 Times in 357 Posts
    what should $offline['a'] contain, a single value from the `a` field, or all its values? you just need to write some code that fetches that into this variable.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    CBG
    CBG is offline
    New Coder
    Join Date
    Feb 2010
    Location
    UK
    Posts
    91
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Column a in abc_offline would have the value as offline or online
    So what I want to do is, in any file be able to have

    This code could be in any file
    if ( $offline['a'] == 'online' ) { header ('location: index.php'); }
    OR
    if ( $offline['a'] == 'offline' ) { header ('location: offline.php'); }

    I need the code to go in the init.php as I haven't got a clue what to put, to be able to able to call the table and assign $offline to any file.

  • #6
    New Coder
    Join Date
    Aug 2003
    Location
    Derby, UK
    Posts
    97
    Thanks
    0
    Thanked 14 Times in 14 Posts
    Something like

    PHP Code:
    $rstOffline mysql_query('SELECT a FROM abc_offline');
    $offline mysql_fetch_assoc($rstOffline); 
    should be all you need if the database is already connected and selected. Assuming init.php is not inside a function this variable when then exist in a global scope.

    When refering to it elsewhere you can either refer to it directly as $offline['a'] or if you are inside a function then either refer to it as $GLOBALS['offline']['a'] or at the top of the function put

    global $offline;

    Note that global variables should generally be used very sparingly, but this is the simplest answer to your direct question. A better solution may be to put the value into session or make it part of a "user" object that gets passed around, hard to say without knowing more about your code.

    HTH,

    Dai

  • #7
    CBG
    CBG is offline
    New Coder
    Join Date
    Feb 2010
    Location
    UK
    Posts
    91
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Thanks, will give it a try later.


  •  

    Posting Permissions

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