scope of mysql_connect?

Posted on 2009-02-19
Medium Priority
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

Loganathan Natarajan earned 1000 total points
ID: 23689267

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

Phatzer earned 1000 total points
ID: 23690040
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month17 days, 9 hours left to enroll

830 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