Magento 2 How to select fields in query
Try following way:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $resource = $objectManager->get('Magento\Framework\App\ResourceConnection'); $connection = $resource->getConnection(); $select = $connection->select() ->from( ['ce' => 'customer_entity'], ['firstname', 'lastname', 'email'] ); $data = $connection->fetchAll($select);
Use below code for select particular field from database table.
File path: magento/app/code/Venodr/ModuleName/Model/CustomerData.php
<?php
namespace Venodr\ModuleName\Model;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\EntityManager\Operation\ExtensionInterface;
class CustomerData
{
protected $connection;
public function __construct(
\Magento\Framework\App\ResourceConnection $resource
)
{
$this->connection = $resource->getConnection();
}
public function getQueryData()
{
$query = $this->connection->fetchAll("SELECT firstname, lastname, email FROM customer_entity");
return $query;
}
}