How to Fetch Data from Magento2 Tables in .phtml files?

  • author-img Nidhi Arora
  • 5 years

Today we’re going to cover fetching of data from Magento 2 tables. We can use the following codes for fetching such data via object manager. Here is how to complete this fetching process successfully:

$this->_resources = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\App\ResourceConnection');

$connection= $this->_resources->getConnection();

$themeTable = $this->_resources->getTableName('converge_token');

$sql = "select * from " . $themeTable . “where customer_id='$customerId'";

$result = $connection->fetchAll($sql);

If you follow the code mentioned above, you will be able to easily fetch data from Magento 2 tables. Keep looking out for our upcoming “How to” blog posts on Magento 2. If you have any issue or query regarding this “How to” blog, feel free to let us know via

Download Blog


Ready to Get started

Communication is the key for us to understand each other. Allow us to understand
your requirements or queries. Present us with an opportunity to serve you.

Fill out the form and out team will get back to you
within 24 hours

    Head Office

    1250 Pittsford Victor Road Ste 310
    Pittsford, NY 14534

    Development Center

    Plot no. 10, Rajiv Gandhi
    Chandigarh Technology Park