using global variables in the database connection string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • frothpoker

    using global variables in the database connection string

    Guys,

    I'm sure this has been asked a million times but I can't seem to
    formulate a google search that returns what i'm looking for.

    I've go a dev and live environment. Currently the DB connect string is
    hard coded into each php file which means i'm going to have to change
    every page - Arrgh!!

    If i put the DBonnect into a function and store it in an include file,
    it doesn't seem to work, Also the connetion works with inline PHP
    statements, but I have to repeat it in every function even if they are
    in the main php file??? which doesn't seem right to me..

    I need to either :-

    Store the connection variables in an include file and retrieve them as
    global values

    OR

    Store the connection string in a function in an include file which can
    be accessed by all functions.

    also do I really have to call the dbconnect inside every function.

    Please note, if it makes any difference, I still havn't got my head
    round OO programming in PHP so it is still pretty much procedural. I
    am using PEAR but dont really understand (or care at the moment) about
    what it is doing.

    Thanks in advance

    Aaron

  • Sjoerd

    #2
    Re: using global variables in the database connection string


    frothpoker wrote:[color=blue]
    > [Very long story about how to store database settings][/color]

    I have this database class, where creating an object with
    $db = new Database();
    either connects to the database or uses the existing database
    connection.

    class Database {

    function Database($datab ase = '') {
    $this->user = 'SYSDBA';
    $this->password = 'secret';
    if (empty($databas e)) {
    $this->database =
    'localhost:/var/lib/firebird2/data/pcleden.fdb';
    } else {
    $this->database = $database;
    }
    if (isset($GLOBALS['ibase_dbconn'])) {
    $this->dbconn = $GLOBALS['ibase_dbconn'];
    } else {
    $this->connect();
    }
    }

    function connect() {
    $res = ibase_connect($ this->database,
    $this->user,
    $this->password);
    $GLOBALS['ibase_dbconn'] = $res;
    $this->dbconn = $res;
    }

    function query($sql) {
    return ibase_query($th is->dbconn, $sql);
    }
    }

    Comment

    • Juliette

      #3
      Re: using global variables in the database connection string

      frothpoker wrote:[color=blue]
      > Guys,
      >
      > I'm sure this has been asked a million times but I can't seem to
      > formulate a google search that returns what i'm looking for.
      >
      > I've go a dev and live environment. Currently the DB connect string is
      > hard coded into each php file which means i'm going to have to change
      > every page - Arrgh!!
      >
      > If i put the DBonnect into a function and store it in an include file,
      > it doesn't seem to work, Also the connetion works with inline PHP
      > statements, but I have to repeat it in every function even if they are
      > in the main php file??? which doesn't seem right to me..
      >
      > I need to either :-
      >
      > Store the connection variables in an include file and retrieve them as
      > global values
      >
      > OR
      >
      > Store the connection string in a function in an include file which can
      > be accessed by all functions.
      >
      > also do I really have to call the dbconnect inside every function.
      >
      > Please note, if it makes any difference, I still havn't got my head
      > round OO programming in PHP so it is still pretty much procedural. I
      > am using PEAR but dont really understand (or care at the moment) about
      > what it is doing.
      >
      > Thanks in advance
      >
      > Aaron
      >[/color]


      Aaron,

      I'm trying to figure out what you are trying to ask, this is what I
      figure so far:

      1. You want to have an easy way to change your connection variables
      between your development and live environment.

      Suggestion:
      At the start of each file which makes a db connection:

      include( path/to/file/dbconfig.inc );

      In dbconfig.inc all you have is:
      <?php
      $dbuser = 'username';
      $dbpass = 'password';
      $dbhost = 'yourhost';
      $dbname = 'database_name' ;
      ?>

      Presuming you use a mysql database, you make a connection like this:

      $db = mysql_connect($ dbhost, $dbuser, $dbpass);
      mysql_select_db ($dbname);

      Of course the dbconfig.inc file which you use for the server will have
      different values for the variables than the one you use for your local
      setup.

      You could even add the connection making statements to the included
      file, just don't put it in a function call within the included file.


      2. You seem to think you need to re-make your connection each and every
      time you make a db call.

      You don't.
      If you make a query call to the database from within a function, do this:

      function myfunction($arg 1, $arg2) {
      global $db;
      ... (whatever else you want to do in the function)
      }

      If that's not enough, identify your connection explicitly:
      mysql_query ( 'SQL statement', $db );

      Hope this helps,
      Juliette

      Comment

      • frothpoker

        #4
        Re: using global variables in the database connection string

        Thanks for that, I managed to find the include file solution elsewhere
        but thanks anyway...

        The global db$ is what I was looking for.....

        Cheers guys

        Juliette wrote:[color=blue]
        > frothpoker wrote:[color=green]
        > > Guys,
        > >
        > > I'm sure this has been asked a million times but I can't seem to
        > > formulate a google search that returns what i'm looking for.
        > >
        > > I've go a dev and live environment. Currently the DB connect string is
        > > hard coded into each php file which means i'm going to have to change
        > > every page - Arrgh!!
        > >
        > > If i put the DBonnect into a function and store it in an include file,
        > > it doesn't seem to work, Also the connetion works with inline PHP
        > > statements, but I have to repeat it in every function even if they are
        > > in the main php file??? which doesn't seem right to me..
        > >
        > > I need to either :-
        > >
        > > Store the connection variables in an include file and retrieve them as
        > > global values
        > >
        > > OR
        > >
        > > Store the connection string in a function in an include file which can
        > > be accessed by all functions.
        > >
        > > also do I really have to call the dbconnect inside every function.
        > >
        > > Please note, if it makes any difference, I still havn't got my head
        > > round OO programming in PHP so it is still pretty much procedural. I
        > > am using PEAR but dont really understand (or care at the moment) about
        > > what it is doing.
        > >
        > > Thanks in advance
        > >
        > > Aaron
        > >[/color]
        >
        >
        > Aaron,
        >
        > I'm trying to figure out what you are trying to ask, this is what I
        > figure so far:
        >
        > 1. You want to have an easy way to change your connection variables
        > between your development and live environment.
        >
        > Suggestion:
        > At the start of each file which makes a db connection:
        >
        > include( path/to/file/dbconfig.inc );
        >
        > In dbconfig.inc all you have is:
        > <?php
        > $dbuser = 'username';
        > $dbpass = 'password';
        > $dbhost = 'yourhost';
        > $dbname = 'database_name' ;
        > ?>
        >
        > Presuming you use a mysql database, you make a connection like this:
        >
        > $db = mysql_connect($ dbhost, $dbuser, $dbpass);
        > mysql_select_db ($dbname);
        >
        > Of course the dbconfig.inc file which you use for the server will have
        > different values for the variables than the one you use for your local
        > setup.
        >
        > You could even add the connection making statements to the included
        > file, just don't put it in a function call within the included file.
        >
        >
        > 2. You seem to think you need to re-make your connection each and every
        > time you make a db call.
        >
        > You don't.
        > If you make a query call to the database from within a function, do this:
        >
        > function myfunction($arg 1, $arg2) {
        > global $db;
        > ... (whatever else you want to do in the function)
        > }
        >
        > If that's not enough, identify your connection explicitly:
        > mysql_query ( 'SQL statement', $db );
        >
        > Hope this helps,
        > Juliette[/color]

        Comment

        Working...