Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKelvin Atawura
Front End Web Development Techdegree Student 19,022 Pointsserver switch statement
l am getting an error in my wordpress dev saying database not found. l have done all the work of importing the up to date database. but its not working and l have a feeling its down to this switch statement in the config file. is there any idea why this is not passing guys? any suggestion would be greately appreciated. thanks
$_SERVER['SERVER_NAME'] = 'localhost';
switch($_SERVER['SERVER_NAME']):
case "elance-assure":
/** The name of the database for WordPress */
define('DB_NAME', 'elance');
/** MySQL database username */
define('DB_USER', 'elance');
/** MySQL database password */
define('DB_PASSWORD', 'elance');
/** MySQL hostname */
define('DB_HOST', 'localhost');
default:
define('DB_NAME', 'expatass_main');
/** MySQL database username */
define('DB_USER', 'expatass_user');
/** MySQL database password */
define('DB_PASSWORD', '$}yxr0M+GG4O');
/** MySQL hostname */
define('DB_HOST', 'localhost');
endswitch;
1 Answer
Lewis Cowles
74,902 PointsDude your assigning rather than checking $_SERVER['SERVER_NAME'] in the first line (comment that out).
It's also really not a great idea to deal directly with $_SERVER, or any other super-global in PHP; I know it might seem like you are going around the houses, by not doing this, but it will make your code more DRY and more widely applicable.
I Would also suggest that DEFINES may not be the best way to handle database credentials (in fact it may be one of the worst ways because anyone can type the define DB_PASS and echo it out to users etc).
If you have to do this, save yourself the switch statement and load a JSON file, then directly address the container you want
{
"default": {
"name": "...",
"user": "...",
"pass": "...",
"host": "...",
"port": "..."
},
"elance-assure": {
"name": "...",
"user": "...",
"pass": "...",
"host": "...",
"port": "..."
}
}
You can then use PHP to read the file as an array or object (personal preference) pass a default argument of 'default' and get the credentials you need. (FYI still not the best way to handle DB creds for an app)
Sample Hack (Please don't use in production, it's not that good, just better than switch-case hardcoded)
<?php
function getDBCredentials( $site='default' ) {
$creds = json_decode( file_get_contents( '/path/to/jsonfile.json' ), true );
if( !is_array( $creds ) ) { return null; } // Maybe throw exception, error whatever for logging
if( !array_key_exists( 'default', $creds ) ) { return null; } // Maybe throw exception, raise error, whatever for logging
$site = array_key_exists( $site, $creds ) ? $site : 'default';
return $creds[ $site ];
}
$isServerDefault = ( stripos( $_SERVER['SERVER_NAME'], 'elance-assure' ) !== false ) ? false : true;
$dbCreds = getDBCredentials( ($isServerDefault ? 'default' : 'elance-assure' ) );
Kelvin Atawura
Front End Web Development Techdegree Student 19,022 PointsKelvin Atawura
Front End Web Development Techdegree Student 19,022 PointsThanks for your reply mate. l do understand where you are coming from but am working within a team and they are used to using the switch statement above so l dont want to be using anything different. Please if there anything l can correct up there please let me know. l am also experiencing an issue where the url says localhost/mysite/ instead of localhost:8888/mysite. any ideas how l can fix this please?
Lewis Cowles
74,902 PointsLewis Cowles
74,902 PointsHi Kelvin
if your team have the slightest competency in development and systems design, I believe they should already know the switch is a bad idea because hard-coding values for access credentials or data that can change is a bad practice. What if you need to split to use more than one DB server? What if the DB get's hacked? What if you switch hosting providers?
At the least wrap it in a function and return the credentials rather than defining them, it's a tripple P the way it is written above. It is also quite poor practice to simple switch out a database within an application to make it work on two urls. You and your team should look into virtualhosts with separate folders. Please tell me you do not edit code in-place on the site?
On the port problem, are you using absolute links to the sites URL's? Again this is horrendous practice, I would love to be able to put it more mildly, but it's another tripple P. Only external links need absolute URL's there is a data, portability and production cost to using absolute URL's when not needed. It should not happen. Using relative links should preserve the port automatically.