0

I have an api written in PHP for a backend service that supports my Android app. I'm in the process of setting up different environments - development, staging, production - each with they're own database. The API performs queries to its respective MySQL database and everything is up and running just great in development.

My question is about the proper technique for handling different database connections for the api in each environment? Is there an easier way to change the database that my php->mysql connections use than changing them all by hand manually before deploying from, say, development to staging?

I'm coming from an Android environment, so what comes to mind right away is Gradle's build flavors and how that allows different code to be used based on the specific build variant you want.

2 Answers 2

1

I think that's a good use case for Apache environment variables, which can be set in the httpd.conf file (or an .htaccess file).

Basically, you can something like:

SetEnv DATABASE_NAME "MyDB"

in your httpd.conf file, and then in PHP use:

$_SERVER['DATABASE_NAME']

to access it.

In general, it's good practice to keep environment-specific things out of your code so that your code is more portable. Your case is a perfect example.

Edit (by rpm): Be sure to restart apache after modifying the httpd.conf file. I had issues getting this answer to work, but restarting Apache fixed everything.

Sign up to request clarification or add additional context in comments.

9 Comments

This is exactly what I was looking for. Thanks!
@Barmar Im having trouble getting this to work. In my httpd.conf file I have SetEnv ENVVAR “hello” and my php file contains <?php echo $_SERVER[‘ENVVAR’]; ?>. This should print out the variable to postman when I hit the endpoint, but Im getting nothing. Thoughts?
@rpm Why are you asking me for help, this is Jason's answer.
@rpm Don't use curly quotes. Turn off "smart quotes" in your text editor when editing program and configuration files.
@Barmar sorry about the accidental mention... regardless, replacing curly quotes didn't change anything.
|
0

What we do is put the database connection parameters in a separate include file, e.g.

$db_host = "xxx";
$db_user = "xxx";
$db_password = "xxx";
$db_database = "xxx";

Then the other scripts can use

require_once('db_params.php');

and use these variables when making the database connection.

Then we just use different contents of the script in different environments.

4 Comments

This is a great solution when you aren't able to access/edit your config files.
We also have different servers and usernames for different applications, and we have read-only users versus read-write users. We have different include files for different purposes, this would probably get out of hand using server environment variables.
I'm not sure that I agree, but it may just be a difference in taste. With environment variables, you can set them per domain, and even per directory.
@Jason We can have different requirements in the same directory. Some web pages only display data, so they use the read-only credentials, while other pages update and use the read-write credentials. Even within a single script it might need to access different database servers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.