Thursday, 23 June 2016

select query Magento 2

<?php
                        $post_categroy='';
                        $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
                        $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
                        $connection = $resource->getConnection();
                        $tableName = $resource->getTableName('magefan_blog_post_category');
                        $sql = "SELECT category_id FROM ".$tableName." WHERE post_id=".$_post->getId();
                        $result = $connection->fetchOne($sql);
                        if(count($result)>0):
                            $tableName2 = $resource->getTableName('magefan_blog_category');
                            $post_catid     =   $result[0];
                            if($post_catid!=''){
                                $sql2 = "SELECT title FROM ".$tableName2." WHERE category_id=".$post_catid;
                                $post_categroy = $connection->fetchOne($sql2);
                               
                            }
                           
                        endif;
                    ?>

No comments:

Post a Comment