8

I want to access the product names by query database using product ID. How can I achieve it?

Fabian Schmengler
66.2k25 gold badges191 silver badges422 bronze badges
asked Dec 21, 2016 at 11:26
4
  • u want sql query or Magento default way Commented Dec 21, 2016 at 11:32
  • i want sql query . I know the default way. Commented Dec 21, 2016 at 11:33
  • why do you want using SQL query? Commented Dec 21, 2016 at 11:42
  • I want to access the product names outside magento without using api. Commented Dec 21, 2016 at 11:43

5 Answers 5

18

Magento uses an EAV table structure for products. Names are stored in catalog_product_entity_varchar. If you have the product ID X, you can query this table directly:

SELECT entity_id, value, store_id FROM catalog_product_entity_varchar
 WHERE entity_id = X AND attribute_id = (
 SELECT attribute_id FROM eav_attribute
 WHERE entity_type_id=4 AND attribute_code='name'
 )

The subquery determines the attribute id for "name", entity_type_id 4 is always the type ID for products.

Note that in a multistore setup you might get multiple values per product. Pay attention to the "store_id" column. "0" stands for the default value.

answered Dec 21, 2016 at 11:44
0
4

If your product's id is 1234:

select value from catalog_product_entity_varchar left join eav_attribute on
 eav_attribute.attribute_id = catalog_product_entity_varchar.attribute_id
where
 eav_attribute.attribute_code='name' and 
 catalog_product_entity_varchar.entity_id=1234
Chirag Prajapati
2,9522 gold badges20 silver badges44 bronze badges
answered Dec 21, 2016 at 11:49
4
  • there is no row_id column, use entity_id instead of row_id. Commented Dec 21, 2016 at 12:04
  • In Magento 2 you have row_id Commented Dec 21, 2016 at 12:21
  • what? can you share screenshot of your database table? Commented Dec 21, 2016 at 12:21
  • Magento EE has row_id. Commented Feb 1, 2017 at 0:03
1

Use below query to find name from product_id

SELECT value FROM catalog_product_entity_varchar WHERE entity_id=2 AND (attribute_id IN(SELECT attribute_id FROM eav_attribute WHERE attribute_code = "name" AND entity_type_id = 4))

Change entity_id=2 as per your product id

answered Dec 21, 2016 at 11:47
1
$_connection = $this->mc_get_obj(\Magento\Framework\App\ResourceConnection::class);//get class
$db_connection = $_connection->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);//connection
$category = $db_connection->fetchAll('select * from '.'`catalog_product_entity_varchar` '.'left join '.'`eav_attribute` '.'on '.
'`eav_attribute`.'.'`attribute_id`='.'`catalog_product_entity_varchar`.'.'`attribute_id` '.'where '.'`eav_attribute`.'.'`attribute_code`='.'"name" '.'and '. '`catalog_product_entity_varchar`.'.'`entity_id`='.$vi['product_id']);
answered Jul 23, 2019 at 15:08
2
  • ------ sql: select * from catalog_product_entity_varchar left join eav_attribute on eav_attribute.attribute_id = catalog_product_entity_varchar.attribute_id where eav_attribute.attribute_code='name' and catalog_product_entity_varchar.entity_id=2082 Commented Jul 23, 2019 at 15:10
  • public function mc_get_obj($class) { return \Magento\Framework\App\ObjectManager::getInstance()->get($class); } Commented Jul 23, 2019 at 15:12
0

Or you can try this also,for single record, X is your product_id you have to replace it by your own product id.

SELECT DISTINCT value FROM catalog_product_entity_varchar WHERE entity_id=X AND (attribute_id IN(SELECT attribute_id FROM eav_attribute WHERE attribute_code = "name" AND entity_type_id = 4))
answered May 25, 2019 at 5:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.