Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,096
    Thanks
    15
    Thanked 1 Time in 1 Post

    Thumbs up setup database connection include file in PHP

    Code:
    <?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:

    Code:
    <?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

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    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.

  • #3
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,096
    Thanks
    15
    Thanked 1 Time in 1 Post
    how do you mean the full file path? the full path would be C:\Program Files\Apache etc.....???

  • #4
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,096
    Thanks
    15
    Thanked 1 Time in 1 Post
    Code:
    <!--#include file="inc/footer.asp"-->
    in ASP i used to do that, but is that not possible in PHP...doing something similar?

  • #5
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,096
    Thanks
    15
    Thanked 1 Time in 1 Post
    my structure is:

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

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

  • #6
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    PHP Code:
    <?php include("inc/db_conn.php"); ?>

  • #7
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,096
    Thanks
    15
    Thanked 1 Time in 1 Post
    cheers for that, will that work ok if i'm in a directiry down from inc?

  • #8
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Try this
    PHP Code:
    <?php include($_SERVER['DOCUMENT_ROOT']."/mini/inc/db_conn.php"); ?>
    ||||If you are getting paid to do a job, don't ask for help on it!||||


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •