Here is a tip I found when fixing the database error for one of my clients.
<?php $row_parts = array("Monthly", "Special", "Transport", 10, 20); foreach ($row_parts as $row_part): foreach ($row_parts as $row_part_2): if ($row_part == $row_part_2): continue; elseif (!is_numeric($row_part) && !is_numeric($row_part_2)): continue; elseif (is_numeric($row_part) && is_numeric($row_part_2)): continue; endif; echo $row_part . "_" . $row_part_2 . "<br />"; endforeach; endforeach; ?>
The above code outputs the following:
It uses a single array to store both words and numbers. Then combine them together through a nested foreach loop with an underscore to form reversible variables, reversible columns in this case.
I then can use these generated values to match the existing columns in the MySQL table and assign the corresponding variables to these values. For example, “20_Monthly” column would assign to $monthly_20 if “Monthly_20″ column is undefined.
It saves client’s, other developers’, and my time to look for the source of errors in the future if the database column change from “20_Monthly” to “Monthly_20″, assuming that the format of the database column names remain the same.
This type of error for changing the name of database column is often occurred during the server migration. An unexperienced client would manually type the database entries by himself or by hiring an assistant, instead of using export and import features in phpMyAdmin.
Have you had similar clients? And how did you respond to them? Discuss them in the comments section below.