0

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

mitchellsk
  • 243
  • 2
  • 5
  • 10

1 Answers1

3

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);
Vishwas Soni
  • 1,567
  • 14
  • 34
  • 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 : http://magento.stackexchange.com/questions/88128/how-to-call-direct-sql-queries-and-join-to-collection-in-magento2 – Vishwas Soni Jun 15 '16 at 06: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