I created a custom db table inside my Magento2 database and am having trouble making my custom phtml file display the field. (I'm new to M2 and php)

Here's my code and when I echo $result it display properly on the website but when I try to display the field with the last line it breaks the page:

enter image description here

1 Answer 1


I think your code is perfect except table name. Try below code.

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

//Select Data from table
$sql = "Select * FROM " . $tableName;
$result = $connection->fetchAll($sql);
  • 1
    That doesn't work because when you add $resource->getTableName it alters "custom_catalog_product_other" to make it "mg2_custom_catalog_product_other".
    – mitchellsk
    Jun 14, 2016 at 15:10
  • Yes. It will add prefix if you have any. From where you are calling your connection? This que might helps you : magento.stackexchange.com/questions/88128/… Jun 15, 2016 at 6:39
  • I'm just saying that snippet wasn't on there because it adds the prefix and doesn't solve the issue of $result = $connection->fetchAll($sql); breaking the page and not displaying the results. "custom_catalog_product_other" is the correct tablename.
    – mitchellsk
    Jun 15, 2016 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.