Db - Database Structure Information

Get detailed insights into the database structure with PowerLite PDO

This page provides a comprehensive guide on how to use the Db class to retrieve information about the database structure. It includes practical code samples for each method.

Loading the Db class

To use the Db class, you first need to load it within the Dependency Injection (DI) container.

use Migliori\PowerLitePdo\Db;

$container = require_once __DIR__ . '/../src/bootstrap.php';
$db = $container->get(Db::class);

Db::getTables()

This method returns an array of table names from the database if successful, otherwise it returns false.

Method Signature

public function getTables(?string $debug = null): mixed

Arguments Summary

ArgumentTypeDescriptionExample
$debugbool|stringThe Debug modefalse, true or 'silent'

Examples

$tables = $db->getTables();
if ($tables !== false) {
    foreach ($tables as $table) {
        echo "Table: $table\n";
    }
} else {
    echo "Failed to retrieve tables.\n";
}

Db::getColumns()

This method retrieves information about the columns in a given table.

Method Signature

public function getColumns(string $table, int $fetchParameters = PDO::FETCH_OBJ, $debug = false): mixed

Arguments Summary

ArgumentTypeDescriptionExample
$tablestringThe name of the table
users
orders
products
$fetchParameters?intThe fetch style. Defaults to PDO::FETCH_OBJ
PDO::FETCH_OBJ
PDO::FETCH_ASSOC
$debugbool|stringThe Debug modefalse, true or 'silent'

Examples

$columns = $db->getColumns('users');
var_dump($columns);

This will output the detailed column information for the 'users' table. For instance:

array (size=5)
  0 =>
    object(stdClass)[33]
      public 'Field' => string 'id' (length=2)
      public 'Type' => string 'int(11) unsigned' (length=16)
      public 'Null' => string 'NO' (length=2)
      public 'Key' => string 'PRI' (length=3)
      public 'Default' => null
      public 'Extra' => string 'auto_increment' (length=14)
  1 =>
    // ... etc

Db::getColumnsNames()

This method returns the column names of the target table.

Method Signature

public function getColumnsNames(string $table, ?string $debug = null): mixed

Arguments Summary

ArgumentTypeDescriptionExample
$tablestringThe name of the table
users
orders
products
$debugbool|stringThe Debug modefalse, true or 'silent'

Examples

$db = new Db();
$columnNames = $db->getColumnsNames('users');

var_dump($columnNames);

In this example, we are fetching the column names of the 'users' table.