/    Sign up×
Community /Pin to ProfileBookmark

db won’t connect with globals in class

I have a class that connects to the db. I’m using global variables to pass the server, login, password, and database. This works in a function but not in a method. I can print_r and see the properties but still errors out when I try it in the class. Here an example of what works and not.

Doesn’t work:
public function connection(){
require_once(‘includes/DBconn.php’);
$this->_connection = mssql_connect($_GLOBAL[‘server’],$_GLOBAL[‘login’],$_GLOBAL[‘password’]);
if(!mssql_select_db($_GLOBAL[‘database’],$this->_connection)){
die(“Could not select DB”);
}
}

works:
public function connection(){
$this->_connection = mssql_connect(‘server’,’login’,’password’);
if(!mssql_select_db(‘database’,$this->_
connection)){
die(“Could not select DB”);
}
}

I know it’s likely something stupid I’m doing. Let me know.

Thanks.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@chazzyApr 16.2008 — while I'm not going to try to dissuade you from using GLOBALs (which are evil), i think if you put as the first line of the function
[code=php]global $_GLOBALS;[/code] it will work.
Copy linkTweet thisAlerts:
@NogDogApr 17.2008 — If your intent is to use PHP's built in super-global array $GLOBALS, note that its name has no underscore and is plural. If, on the other hand $_GLOBAL is not intended to be that array, then I would just pass it as a function parameter, in order to avoid the "evils" of globalization. ?

[code=php]
function connect($_GLOBAL) {
// etc....
}
[/code]
Copy linkTweet thisAlerts:
@lcraneauthorApr 21.2008 — Thanks for you help. Even though I'm a programmer, I don't want to be an "evil" programmer. ? I don't feel comfortable storing the database, server, login, and password in the class. What would be a better way to pass these variables to the class? Should I store them in a array, pass the array to the connection method, then parse the array for the variables? Would that make me slightly less "evil"? Let me know your opinion.
Copy linkTweet thisAlerts:
@NogDogApr 21.2008 — You could put the connection values in a separate .ini file, putting that file in a directory outside of the web document root. E.g.: if your web document root is "/home/users/icrane/public_html", then save it as "/home/users/icrane/includes/db.ini". Then you could pass its path to your DB class's constructor, and parse it there:
[code=php]
class DB
{
private $dbHost;
private $dbUser;
private $dbPwd;
private $dbName;
private $connx;

public function __construct($iniFilePath)
{
$iniArray = parse_ini_file($iniFilePath);
if(empty($iniArray))
{
throw new Exception("No data from ini file");
}
foreach(array('dbHost', 'dbUser', 'dbPwd', 'dbName') as $key)
{
if(!empty($iniArray[$key]))
{
$this->$key = $iniArray[$key];
}
else
{
throw new Exception("Missing key '$key' in ini file");
}
}
}

public function connect();
{
$this->connx = mysql_connect($this->dbHost, $this->dbUser, $this->dbPwd);
if($this->connx !== false)
{
if(mysql_select_db($this->dbName) === false)
{
user_error(mysql_error());
return false;
}
return true;
}
user_error(mysql_error());
return false;
}
}
[/code]

ini file:
<i>
</i>dbHost="localhost"
dbUser="foobar"
dbPwd="raboof"
dbName="my_database"

Usage:
[code=php]
try {
$db = new DB('/home/users/icrane/includes/db.ini');
}
catch (Exception $e) {
die("Uh-Oh!");
}
if($db->connect() === false)
die("DB connection failed");
[/code]
×

Success!

Help @lcrane spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...