cancel
Showing results for 
Search instead for 
Did you mean: 

Sql query magento 2

SOLVED

Sql query magento 2

Hi

 

I need to run simple sql query with fetchOne in magento 2

How I can do code for it

 

Can someone help.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Sql query magento 2

Hi @johdanlusefa34 

 

Please try with the below code:

 

protected $_resource;
$this->_resource = $resource;
$table=$this->_resource->getTableName('tablename'); 
$this->_connection = $this->_resource->getConnection();
$query = 'SELECT * FROM '.$table;

$collection = $this->_connection->fetchOne($query );
Hope this helps you!
Problem Solved! Click Kudos & Accept as Solution!

View solution in original post

1 REPLY 1

Re: Sql query magento 2

Hi @johdanlusefa34 

 

Please try with the below code:

 

protected $_resource;
$this->_resource = $resource;
$table=$this->_resource->getTableName('tablename'); 
$this->_connection = $this->_resource->getConnection();
$query = 'SELECT * FROM '.$table;

$collection = $this->_connection->fetchOne($query );
Hope this helps you!
Problem Solved! Click Kudos & Accept as Solution!