PHP: 2 includes, but only the first one works?

Posted on 2007-08-03
Last Modified: 2008-02-01
I made some simple php scripts and run into a problem with the include functions and mySQL connections.


If I execute test.php, it executes nav.php but crashes on nav1.php, when this tries to connect to the database.

If I change the order of the includes in test.php, nav1 gets executed easily, but nav.php crashes when connecting to the database.

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'www-data'@'localhost' (using password: NO) in /var/www/vhosts/ on line 42
Could not connect: Access denied for user 'www-data'@'localhost' (using password: NO)

As mentioned: If I change the order of the includes, it works fine, so both scripts and settings are ok.
Any idea what can cause this issue?

Question by:Smoerble
    LVL 14

    Expert Comment

    by:Aamir Saeed
    it might be due to include_once, try using include()
    LVL 2

    Expert Comment

    I don't think that include_once versus include would make a difference, even if you were including the same file (which you're not).

    I think the problem is that nav1.php sets some variables and then connects to the database.  The other file, nav.php just tries to connect to the database.

    If you include them in one order (nav1 and then nav),  then the variables ($userid and $password probably) are set and they are reused when nav is included.

    If you include them in the other order (nav and then nav 1 which is what you are trying to do), the variables aren't set yet when nav tries to connect to the database.
    LVL 2

    Accepted Solution

    (So just put those variables at the top of nav and it should work).  If the variables are coming from an include file as well, then you can include that config file at the top of BOTH nav and nav2 but use include_once.

    You should be deliberate about whether you mean to use include or include_once when you bring in a file.  Config files should be include_once.  Files that DO stuff (like display a nav) should usually be brought in with include.

    Author Comment

    Thank you, that worked perfectly.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    undefined index setting cookie 11 29
    What does this mean? 6 25
    Regex rule to match two different url 5 10
    MySQL Error 3 4
    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now