Many times you need to fetch table data from Magento 2 database to customize Magento 2 store according to your requirements therefore we are going to explain how can you fetch table data From Database in Magento 2. By using this method you can get data from any table here you can see that we are fetching customer table data from Magento 2 database using the custom module and display on the frontend.
Step 1: You need to create a basic module. you can follow this link given below to create a Magento 2 module. Here Webiators is the vendor name and the module is FetchTableData
Step 2: Now after creating the basic module of Magento 2 you need to create a route file and add this code given below
app/code/Webiators/FetchTableData/etc/frontend/routes.xml
1 2 3 4 5 6 7 8 | <?xml version="1.0" ?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd"> <router id="standard"> <route frontName="fetchdata" id="fetchdata"> <module name="Webiators_FetchTableData"/> </route> </router> </config> |
Step 3: Create a controller and action file path
app/code/Webiators/FetchTableData/Controller/Index/Index.php
add this code given below in this file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php namespace Webiators\FetchTableData\Controller\Index; class Index extends \Magento\Framework\App\Action\Action { protected $_pageFactory; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $pageFactory) { $this->_pageFactory = $pageFactory; return parent::__construct($context); } public function execute() { return $this->_pageFactory->create(); } } |
Step 4: Create a layout file fetchdata_index_index.xml
app/code/Webiators/FetchTableData/view/frontend/layout/fetchdata_index_index.xml
1 2 3 4 5 6 7 | <?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <referenceContainer name="content"> <block class="Magento\Framework\View\Element\Template" name="fetchdata_index_index" template="Webiators_FetchTableData::fetchtabledata.phtml" /> </referenceContainer> </page> |
Step 5: Create a template file fetchtabledata.phtml
app/code/Webiators/FetchTableData/view/frontend/templates/fetchtabledata.phtml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?php $this->_resources = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\App\ResourceConnection'); $connection= $this->_resources->getConnection(); $dataTable = $this->_resources->getTableName('customer_entity'); $sql = "select * from " . $dataTable; $customersData = $connection->fetchAll($sql); foreach ($customersData as $key => $customer) { print_r($customer) . "</br>"; } ?> |
Step 6: Run this following Comman
1 2 3 4 5 6 | php bin/magento setup:upgrade php bin/magento setup:di:compile php bin/magento setup:static-content:deploy -f en_US php bin/magento cache:flush php bin/magento cache:clean php bin/magento indexer:reindex |
Step 7: Check result on frontend
Hope this post will be very helpful for you! Thank You 🙂
Read More