Set up magento 2 database connection script

This tutorial explains how to set up magento 2 database connection script. For importing products or categories programmatically we need to establish a database connection.

The following code establish the database connection programmatically. Click here to find out how to mass import magento 2 categories through script.

use MagentoFrameworkAppBootstrap;

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();

$url = MagentoFrameworkAppObjectManager::getInstance();
$storeManager = $url->get('MagentoStoreModelStoreManagerInterface');
$storeId = $storeManager->getStore()->getStoreId();

Share this Tutorial