Magento2 Database – How to Display a Database Table in .phtml File

databasemagento2

I have created a custom module and created a database table using install schema like below:

id      color     price_component
1       white     0
2       red       4
3       green     8
4       black     12

How can I display this table in my .phtml file?

I'm new to Magento. If someone can explain this step by step that would be really helpful.

Best Answer

You can used this for getting table in phtml file

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('your_table_name');

//Select Data from table
$sql = "Select * FROM " . $tableName;
$result = $connection->fetchAll($sql);
Related Topic