Page 1 of 1

defining a globalize database connection string

Posted: Mon May 08, 2006 9:17 pm
by beckbapegeneral
hi, can anyone of you teach me how to define a globalize database connection string so that i can just call it from any files instead of having to define a new connection string everytime. and also where do i define the global database connection string at?

Posted: Mon May 08, 2006 9:22 pm
by feyd
No matter what, the database will have to be connected to, by the script, on each page request needing the database. If you include() a script that creates the connection for you it can be more simple as far as remembering to start it up.

Posted: Mon May 08, 2006 9:27 pm
by beckbapegeneral
thanks moderMonkey :D

Posted: Tue May 09, 2006 1:13 am
by dibyendrah
Create the file dbconfig.php

Code: Select all

<?php
define("HOST", "HOST_NAME");
define("PORT", 3306);
define("USER", "USER_NAME");
define("PASS", "PASSWORD");
define("DB", "DATABASE_NAME");
?>
After that whichever page needs the connection, just include the dbconfig.php

Code: Select all

include("path/to/dbconfig.php");
$conn = mysql_connect(HOST, USER, PASS, DB, PORT);  //<- This will use the Global Constants
Cheers,
Dibyendra

Posted: Tue May 09, 2006 1:16 am
by beckbapegeneral
thanks alot :D

Posted: Tue May 09, 2006 1:20 am
by RobertGonzalez
Because all of my apps rely on a database connection, I usually develop a DB functions class (PHP4) and instantiate the db object at the end of the class. If the connection is not good, I set the script to die.

I then include this class as an include in a common file and include the common file at the beginning of each page, so that on every page, regardless of where it is in the site, will load, instantiate and check the db connection, dying on failure or continuing upon success.

Posted: Tue May 09, 2006 2:18 am
by dibyendrah
Alternate option is to use ADOdb Database Abstraction Library for PHP (and Python) for MySQL .

http://adodb.sourceforge.net/

Dibyendra