scope of mysql_connect?

Hi,

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

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


// whatever.php
<?php
        include_once '../dbConnect.php';
       
        dbConnect();

        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?

Thanks
DJ_AM_JuiceboxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
Loganathan NatarajanLAMP DeveloperCommented:
hi,

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

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
PhatzerCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.