rwhite35 Posted February 23, 2015 Share Posted February 23, 2015 (edited) Hello All, Have a GoDaddy MySQL PHP:PDO hosting questions. Using a database class to make script queries. Unfortunately, I'm required to use GoDaddy Windows hosting. However, I'm a Linux kind of guy. Here's the issue. The viewer script includes database credentials defined as constants, in a file outside the root directory. Typically, these constants are available to all sub processes including class objects. Everything works as expected on development server. The error message I get with version using constants: L226: projectGallery_class datapull Caught Exception --- invalid data source name with host string userDBName.db.6482519.hostedresource.com;dbname=userDBName However when I deploy to GoDaddy's production server, the constants are defined - but empty. Here is the example code. /* works on Linux */ class datapull { var $host; // not assigned here var $userName; // not assigned here var $userPass; // not assigned here protected function conn() { try { $DB = new PDO(DB_HOST,DB_UNAME,DB_PASS); $DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $DB; } catch( Exception $e ) { $mes = "L226: datapull::conn() Caught Exception --- "; $mes .= $e->getMessage(); error_log($mes); } } public function dbquery($qqueue) { $DB = (!isset($DB)) ? $this->conn() : $DB; /* some query stuff here */ return $result } The modified class for GoDaddy Windows class datapull { var $host = “mysql:host=userDBName.db.1234567.hostedresource.com;dbname=userDBName”; var $userName = “userDBName”; //godaddy user and database naming convention var $userPass = “userPWDString”; //user password protected function conn() { try { $DB = new PDO($this->host,$this->userName,$this->userPass); $DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $DB; } catch( Exception $e ) { $mes = "L226: datapull::conn() Caught Exception --- "; $mes .= $e->getMessage(); error_log($mes); } } public function dbquery($qqueue) { $DB = (!isset($DB)) ? $this->conn() : $DB; /* some query stuff here */ return $result } Edited February 23, 2015 by rwhite35 Quote Link to comment Share on other sites More sharing options...
requinix Posted February 24, 2015 Share Posted February 24, 2015 Typically, these constants are available to all sub processes including class objects.That's not something that happens without you specifically telling PHP to do it. So how did you set that up? Did you remember to do that on the Windows hosting? Quote Link to comment Share on other sites More sharing options...
rwhite35 Posted February 24, 2015 Author Share Posted February 24, 2015 Requinix, I was afraid it may be some configuration issue with GoDaddy/Windows environment. As mentioned before, my PHP Windows / GoDaddy hosting knowledge is limited. I'll research that direction though. Thanks for the reply. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted February 24, 2015 Share Posted February 24, 2015 there's nothing about defined constants that are operation system dependent, though your code concerning the values being assigned to the defined constants could be doing something that's php version/configuration dependent. you would need to debug why the constants are empty. we would need to see the code to help, including the type of php opening tag being used in the file. also, in the posted code, your strings are enclosed by curly/smart quotes, not straight-quotes, so php may not be seeing them as the strings you think they are. edit: also2, - the constants are defined - but empty how do you know they are defined, do you have php's error reporting and display/log errors set so that you would be getting undefined constant notices if they are not defined? Quote Link to comment Share on other sites More sharing options...
rwhite35 Posted February 24, 2015 Author Share Posted February 24, 2015 Thanks Mac Gyver, defined constants could be doing something that's php version/configuration dependent. Okay working off that premise I'm revising my original code to include the (module specific) configuration file: protected function conn() { require '../SITE_CONF.php'; //site wide user config includes db connection credentials try { $DB = new PDO(DB_HOST,DB_UNAME,DB_UPWORD); $DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $DB; } catch( Exception $e ) { $mes = "L226: projectGallery_class.php Caught Exception --- "; $mes .= $e->getMessage(); $mes .= (defined('DB_HOST')) ? " host string ".DB_HOST : " host empty."; error_log($mes); //uses system error report } } SITE_CONF.php is also included in the viewer page - index.php. So I'm getting an already defined, but then in my defined() conditions, you'll see "host empty". PHP Notice: Constant DB_NAME already defined in D:\path\to\domain\SITE_CONF.php on line 66 PHP Notice: Constant DB_UNAME already defined in D:\path\to\domain\SITE_CONF.php on line 67 PHP Notice: Constant DB_UPWORD already defined in D:\path\to\domain\SITE_CONF.php on line 68 PHP Notice: Constant BD_HOST already defined in D:\path\to\domain\SITE_CONF.php on line 71 PHP Notice: Use of undefined constant DB_HOST - assumed 'DB_HOST' in D:\path\to\domain\portfolio\lib\projectGallery_class.php on line 220 L226: projectGallery_class.php Caught Exception --- invalid data source name host empty. PHP Fatal error: Call to a member function setAttribute() on a non-object in D:\path\to\domain\portfolio\lib\projectGallery_class.php on line 243 Finally, here is how the constants are being defined in SITE_CONF.php: /* * DB Credentials * godaddy requires database name and user name the same string */ define("DB_NAME","nameString"); define("DB_UNAME","nameString"); define("DB_UPWORD","passwordString"); define("BD_HOST","mysql:host=nameString.db.1234567.hostedresource.com;dbname=nameString"); Thanks in advance for any help! I appreciate the assist. Quote Link to comment Share on other sites More sharing options...
rwhite35 Posted February 24, 2015 Author Share Posted February 24, 2015 (edited) additional testing: //changed line 10 in first code, get_defined_constants returns 1 - not a list of constants=value $mes .= (defined('DB_HOST')) ? " host string ".DB_HOST : print_r(get_defined_constants(true)); From this, it would seem that the constants are NOT defined for datapull() class. BTW, this is a class being called from an interface->sub class->datapull::conn(). All the machinery is working if I hard code the credential as datapull() class properties. But then portability goes down and I would need to touch several scripts to make the solution work. Edited February 24, 2015 by rwhite35 Quote Link to comment Share on other sites More sharing options...
rwhite35 Posted February 24, 2015 Author Share Posted February 24, 2015 (edited) So this is unexpected. If $host is assigned as a class property, but the db user name and password remain constants defined in SITE_CONF script, it will all work... And while this isn't how I want to do it, its a work-around that will have to do. class datapull { /* class properties */ var $host; protected function conn() { require '../SITE_CONF.php'; //defined $host $this->host=$host; try { $DB = new PDO($this->host,DB_UNAME,DB_UPWORD); //mixed attributes, properties and const $DB->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $DB; } catch( Exception $e ) { $mes = "L226: projectGallery_class.php Caught Exception --- "; $mes .= $e->getMessage(); $mes .= (empty($this->host)) ? " host sting empty!" : null; error_log($mes); //uses system error report } } Edited February 24, 2015 by rwhite35 Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted February 24, 2015 Share Posted February 24, 2015 you have a typo in the DB_HOST (you have BD_HOST) defined constant, did you read the errors you are getting? Quote Link to comment Share on other sites More sharing options...
Solution rwhite35 Posted February 24, 2015 Author Solution Share Posted February 24, 2015 you have a typo in the DB_HOST (you have BD_HOST) defined constant, did you read the errors you are getting? Ugh! Dyslexia is a real b***h when you write code for a living... I was thrown off by thinking it was an issue with godaddy/Windows and failed to look at the simplest things first... Thanks for the assist! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.