You are here: Re: using global variables in the database connection string « PHP Programming Language « IT news, forums, messages
Re: using global variables in the database connection string

Posted by Juliette on 06/20/06 19:18

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация