scope of mysql_connect?

Posted on 2009-02-19
Last Modified: 2012-05-06

If I put a mysql connect function in a separate file from my other scripts, is the connection still valid?:

// dbConnect.php
    mysql_connect($hostname, $username, $password) OR DIE;

// whatever.php
        include_once '../dbConnect.php';

        mysql_query("select * from orange");

So, even though the mysql_connect function is scoped within a sub function, it still makes a valid global resource for all mysql calls to use? So mysql somehow is creating some global variable for the lifetime of the script, even though technically the call was scoped in some sub function?

Question by:DJ_AM_Juicebox
    LVL 36

    Accepted Solution


    actually the  connection is opened at the top of a script should be globally available even to functions and to the pages etc.,
    LVL 10

    Assisted Solution

    Yes, as long as the mysql_connect() is called on every page where there are queries, and before the queries are called in the code then it'll work. Of course changes would have to be made if you decided you wanted to connect to two separate databases, however if there's just one, then the MySQL link resource is available across all scripts that follow the mysql_connect() function.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    734 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

    25 Experts available now in Live!

    Get 1:1 Help Now