I want to access the product names by query database using product ID. How can I achieve it?
-
u want sql query or Magento default wayArshad Muhammad– Arshad Muhammad2016年12月21日 11:32:13 +00:00Commented Dec 21, 2016 at 11:32
-
i want sql query . I know the default way.Nitin Pawar– Nitin Pawar2016年12月21日 11:33:21 +00:00Commented Dec 21, 2016 at 11:33
-
why do you want using SQL query?Chirag Prajapati– Chirag Prajapati2016年12月21日 11:42:19 +00:00Commented Dec 21, 2016 at 11:42
-
I want to access the product names outside magento without using api.Nitin Pawar– Nitin Pawar2016年12月21日 11:43:19 +00:00Commented Dec 21, 2016 at 11:43
5 Answers 5
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.
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
-
there is no
row_idcolumn, useentity_idinstead ofrow_id.Chirag Prajapati– Chirag Prajapati2016年12月21日 12:04:56 +00:00Commented Dec 21, 2016 at 12:04 -
In Magento 2 you have row_idPhoenix128_RiccardoT– Phoenix128_RiccardoT2016年12月21日 12:21:08 +00:00Commented Dec 21, 2016 at 12:21
-
what? can you share screenshot of your database table?Chirag Prajapati– Chirag Prajapati2016年12月21日 12:21:58 +00:00Commented Dec 21, 2016 at 12:21
-
Magento EE has row_id.nikola99– nikola992017年02月01日 00:03:59 +00:00Commented Feb 1, 2017 at 0:03
Use below query to find name from product_id
SELECT
valueFROMcatalog_product_entity_varcharWHERE entity_id=2 AND (attribute_id IN(SELECTattribute_idFROMeav_attributeWHEREattribute_code= "name" ANDentity_type_id= 4))
Change entity_id=2 as per your product id
$_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']);
-
------ sql: select * from
catalog_product_entity_varcharleft joineav_attributeoneav_attribute.attribute_id=catalog_product_entity_varchar.attribute_idwhere eav_attribute.attribute_code='name' and catalog_product_entity_varchar.entity_id=2082jerry xu– jerry xu2019年07月23日 15:10:41 +00:00Commented Jul 23, 2019 at 15:10 -
public function mc_get_obj($class) { return \Magento\Framework\App\ObjectManager::getInstance()->get($class); }jerry xu– jerry xu2019年07月23日 15:12:01 +00:00Commented Jul 23, 2019 at 15:12
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))