Odie Odie - 5 months ago 36
MySQL Question

Select a specific prefix on table depending on a store branch

I have an interbranch inventory system, meaning that I have three branches. Whenever an admin logged in to the system, it will detect the location of his branch. Here is an example of my code:

if ($_SESSION['user_role'] == 'Admin') {
$execLog = mysqli_query($connection, $queryLog);

if ($_SESSION['branch'] == 'BS') {

header("Location: ../admin");

} elseif ($_SESSION['branch'] == 'TD') {

header("Location: ../admin");

} elseif ($_SESSION['branch'] == 'FV') {

header("Location: ../admin");


Each of the branch has a specific prefix for a table on my database. BS is
, TD is
, and FV
. Now, my problem is how do I set a query to the specific table in the database without going all through the tables of each admin?

For example, if my branch is BS, I want the queries to be:

SELECT * FROM bs_orders;

And if my branch is TD, I want the queries to be:

SELECT * FROM td_orders

How do I achieve this?

Anyway, here is my connectdb.php code


$db['db_host'] = "localhost";
$db['db_user'] = "root";
$db['db_pass'] = "";
$db['db_name'] = "inventorydb";

foreach ($db as $key => $value) {
define(strtoupper($key), $value);

$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

if (!$connection) {
echo "Failed to connect to the database" . mysqli_errno($connection);



Use string concatenation.

Set the prefix in a variable like this:

$prefix= strtolower($_SESSION['branch']);

and in the query use

$sql = "SELECT * FROM $prefix.'_orders'"