How to get my custom table data from magento 2

1) I Assume you have created Model and Collection file associated with that tables.

2) In a Block PHP file constructor add one argument (Dependency Injection) like below and store it in a class member variable.

 public function __construct(
    Context $context,
    \Namespace\Modulename\Model\ModelNameFactory $modelNameFactory,

    array $data = array()
) {
    $this->_modelFactory = $modelFactory;
    parent::__construct($context, $data);
}

3) Prepare a public method in your block to access collection like below.

public function getCollection(){

    return $this->_modelFactory->create()->getCollection();

}

4) Loop through each of the collection result.

Hope, this will help you.


You can directly get custom table using objectmanager concept,

    $objectManager =   \Magento\Framework\App\ObjectManager::getInstance();
    $connection = $objectManager->get('Magento\Framework\App\ResourceConnection')->getConnection('\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION'); 
    $result1 = $connection->fetchAll("SELECT * FROM email_format");

echo "<pre>";print_r($result1);

Or

This is proper way using block:

public function __construct(
    Context $context,
    \Namespace\Module\Model\ModuleFactory $modelFactory,

    array $data = array()
) {
    $this->_modelFactory = $modelFactory;
    parent::__construct($context, $data);
}

You can get collection by factory methods:

public function getCollection(){

    return $this->_modelFactory->create()->getCollection();

}