...

View Full Version : setup database connection include file in PHP



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

$dbname = 'crmpicco_mini';
mysql_select_db($dbname);
?>


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,
Picco

Inigoesdr
09-27-2007, 08: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.

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

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


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

crmpicco
09-27-2007, 10:02 PM
my structure is:

/htdocs
>>>>>
/mini
>>>>>
/inc

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

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

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

_Aerospace_Eng_
09-28-2007, 07:46 PM
Try this

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum