By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

using global variables in the database connection string

P: n/a
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

Jun 20 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

frothpoker wrote:
[Very long story about how to store database settings]


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($database = '') {
$this->user = 'SYSDBA';
$this->password = 'secret';
if (empty($database)) {
$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($this->dbconn, $sql);
}
}

Jun 20 '06 #2

P: n/a
frothpoker wrote:
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

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($arg1, $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
Jun 20 '06 #3

P: n/a
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:
frothpoker wrote:
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

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($arg1, $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


Jun 20 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.