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


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);
| improve this answer | |
  • 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 '16 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/… – Vishwas Soni Jun 15 '16 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 '16 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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