View Full Version : setup database connection include file in PHP

09-27-2007, 09:31 PM
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'mypassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'crmpicco_mini';

Is there a way I can have all of this in a database include file at the top of all my scripts? When i try it it just errors. Can only some of it go in an include?

I tried:

<?php include("/mini/inc/db_conn.php"); ?>

It didn't like that, or splitting the code into two seperate files as one site had suggested. Incidentally, do I always need to use "<?php" can't I just have "<?" to open scripting tags?

Cheers in advance,

09-27-2007, 09:56 PM
You should always use <?php because shorttags can be off on some servers, and it's best practice. Your include will work if you remove the first forwardslash or change it to the full file path.

09-27-2007, 10:56 PM
how do you mean the full file path? the full path would be C:\Program Files\Apache etc.....???

09-27-2007, 10:58 PM
<!--#include file="inc/footer.asp"-->

in ASP i used to do that, but is that not possible in PHP...doing something similar?

09-27-2007, 11:02 PM
my structure is:


index.php is inside mini, the db conn files are inside /inc

09-27-2007, 11:24 PM
<?php include("inc/db_conn.php"); ?>

09-28-2007, 08:32 PM
cheers for that, will that work ok if i'm in a directiry down from inc?

09-28-2007, 08:46 PM
Try this

<?php include($_SERVER['DOCUMENT_ROOT']."/mini/inc/db_conn.php"); ?>