---
title: "Magento 2: How to use the fetchOne() Method with SQL Query"
url: "https://magecomp.com/blog/magento-2-use-fetchone-method-with-sql-query/"
date: "2024-12-13T12:45:33+00:00"
modified: "2025-02-12T10:16:24+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 303
reading_time: "2 min read"
summary: "Hello Magento Friends,"
description: "Learn how to efficiently retrieve specific data from your Magento 2 database using the fetchOne() method."
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Use Local &#038; Cookie Storage using JavaScript modules in Magento 2"
    url: "https://magecomp.com/blog/use-local-cookie-storage-using-javascript-modules-in-magento-2/"
  - title: "How To Deal With SEO Traffic Loss Due To Magento 2 Migration?"
    url: "https://magecomp.com/blog/deal-seo-traffic-loss-due-magento-2-migration/"
  - title: "Magento 2: Add Custom Message to Admin Sales Order View, Invoice, and Credit Memo"
    url: "https://magecomp.com/blog/magento-2-add-custom-message-to-admin-sales-order-view-invoice-credit-memo/"
---

# Magento 2: How to use the fetchOne() Method with SQL Query

_Published: December 13, 2024_  
_Author: Dhiren Vasoya_  

![How to use the fetchOne() Method with SQL Query in m2](https://magecomp.com/blog/wp-content/uploads/2024/12/How-to-use-the-fetchOne-Method-with-SQL-Query-in-m2-1024x512.webp)

**Hello Magento Friends,**

SQL queries in Magento 2 can be extensively employed to tailor and extend platform functions. One common way involves fetching a single value from a database table through the fetchOne() method.

[![Magento Malware Removal](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-Malware-Removal-CTA-2.webp)](https://magecomp.com/magento-malware-removal.html)The fetchOne() method is a part of Magento’s database abstraction layer, which provides a consistent way to interact with different database systems. When you execute an SQL query using fetchOne(), it returns the first column of the first row of the result set. If the query doesn’t return any results, it returns null.

## Steps to use the fetchOne() Method with SQL Query:
**Step 1:** Create **fetch_record.php** file in the Magento root path and then add the below code.

```
<?php

use MagentoFrameworkAppBootstrap;

try{
 	require __DIR__ . '/app/bootstrap.php';
}catch (Exception $e){
	echo 'Autoload error: ' . $e->getMessage();
	exit(1);
}
try{

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

	$state = $obj->get('MagentoFrameworkAppState');
	$state->setAreaCode('frontend');

	$resourceConnection = $obj->get('MagentoFrameworkAppResourceConnection');

	$tableName = $resourceConnection->getTableName('sales_order');

	$connection = $resourceConnection->getConnection();

	$increment_id = '000000001'; //Add Your Ordert increment_id

	$select = $connection->select()
	    ->from(['c' => $tableName], ['*'])
	    ->where("c.increment_id = :increment_id");
	$bind = ['increment_id' => $increment_id];

	$result = $connection->fetchOne($select, $bind);

	if ($result) {
	    echo "Fetched value: " . $result;
	} else {
	    echo "No record found for Increment Id: " . $increment_id;
	}

}catch(Exception $e){
    print_r($e->getMessage());
}
```

[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Speed-Performance-Optimization-Services-CTA-2.webp)](https://magecomp.com/magento-performance-optimization.html)**Step 2:** After adding the above code, run the below URL to check whether the value is fetched or not.

**https://yourdomain/fetch_record.php**

## Conclusion:
Hence, this way you can use the fetchOne() Method with SQL Query. If you have any doubts let me know through the comment section and stay updated with us for more such Magento 2 tutorials.

[![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-1.webp)](https://magecomp.com/services/hyva-theme-development/)**Happy Coding!**


---

_View the original post at: [https://magecomp.com/blog/magento-2-use-fetchone-method-with-sql-query/](https://magecomp.com/blog/magento-2-use-fetchone-method-with-sql-query/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-09-22 10:12:07 UTC_  
