Using Global DB Config file in PHP

At times we do face problems modifying names of a table or a column after the application has been scaled. This is more problematic when working with large applications as I have learnt from experience. It generally requires a lot of rework with existing codes.

So lately, in my PHP projects I have been using global_db_config file  to define each table and column names. In the application we can use variable names rather than the original names of tables and columns. This ensures that if we need to change table or column name in future, we can do so in theglobal_db_config file only. There would be no need to revisit the code and modify them.

Suppose we have a table called “employee” and it has 3 columns as “id”, “name”,”email” our global_db_config  file can hold


So in PHP if you are trying to select all employes names from table the query will change to

mysql_query(“select “.COL_EMP_ID.”,”.COL_EMP_NAME.”,”.COL_EMP_EMAIL.” from “.TAB_EMPLOYEE);

So if in future you can change the “name” field to “employee_name” and there would be no need to change the query. You just have to change the line inglobal_db_config 

from define(COL_EMP_NAME,’name’); to define(COL_EMP_NAME,’employee_name’);

This approach can greatly decrease rework as well as the need to retest the application.

However, if you have a large database (for eg. 100 tables with 100s of columns) creating a config file can be difficult.

The following code can be used to automatically generate global_db_config, just copy/save the code to a file named “generate_db_config.php” and run the script.


 
HOST :
ROOT :
PASSWORD :
DB NAME :
150 150 Burnignorance | Where Minds Meet And Sparks Fly!