database name variable (v2023)

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
philmills
User
Posts: 566

database name variable (v2023)

Post by philmills »

I'm building a custom page which queries table and field names from the current database, and performs certain actions on the tables.
I don't want to hardcode the database name into a variable.
I recall that there is a global var $DB which should be the database name, but it seems it doesn't work.
Is there a global var for the database name which can be called from a custom page?

I'm trying to get all the table names and their fields from the database into an array. Not running queries on the data.
The code works, but only if i manually add the database name like:
$row['Tables_in_MyDatabase']

// Populate the array with table names and fields
foreach ($tables_result as $row) {
    $table_name = $row['Tables_in_' . $mydb]; // Assuming $mydb contains the database name
    $fields_query = "SHOW COLUMNS FROM $table_name";
    $fields_result = ExecuteRows($fields_query);

    // Initialize an array to store fields for the current table
    $table_fields = array();

    // Populate the table_fields array with field names
    foreach ($fields_result as $field_row) {
        $field_name = $field_row['Field'];
        $table_fields[] = $field_name;
    }

    // Add the table name and its fields to the tables_and_fields array
    $tables_and_fields[$table_name] = $table_fields;
}

arbei
User
Posts: 9434

Post by arbei »

  1. Read Database, Table and Field Variable Names. From the UI you know all the database variables name, then you may use them in your code.
  2. Alternatively, you may refer to the connection info generated in config.<environment>.php.

mobhar
User
Posts: 11765

Post by mobhar »

If your PHPMaker project is only using one database (there is no Linked Tables), then you may try this code to get your database name:

<?php
    $db = Config("Databases.DB");
    echo "Database Name: " . $db["dbname"];
?>

philmills
User
Posts: 566

Post by philmills »

Perfect! Thank-you so much!


Post Reply