Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Custom SQL Queries
 
webfxmasta
Jr. Member
 
Total Posts:  15
Joined:  2008-02-29
 

How do I run raw SQL queries, inside a Magento template file (.phtml) for example, using the Magento Models?
I want to run a custom query and display something on the homepage inside a custom template file that I made.

 
Magento Community Magento Community
Magento Community
Magento Community
 
winzippy
Sr. Member
 
Avatar
Total Posts:  101
Joined:  2008-02-08
Rochester, NY, USA
 

I’d like to know too. Specifically in catalog/category/view.phtml.

 
Magento Community Magento Community
Magento Community
Magento Community
 
alistek
Sr. Member
 
Total Posts:  293
Joined:  2008-04-02
Normal, IL
 

Here is one way to do it, but I can’t remember if this is from phtml files or from the Model itself.

$w Mage::getResourceSingleton(’core/resource’)->getConnection(’core_write’);
$result $w->query(’select â€™entity_id’ from â€™catalog_product_entity’);
if (!
$result{
return false;
}
$row 
$result->fetch(PDO::FETCH_ASSOC);
if (!
$row{
return false;
}

-Adam

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

Do you really need to run custom SQL? 
Is it impossible to solve the problem using the Magento model code?

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

Sorry if I came across the wrong way.  I was just wondering what you were doing that needed you to execute raw SQL. 

I always get worried when I see people skirt around the framework already in place.  While it isn’t always easy to do it with a framework, you get that abstraction and your code is the same os the rest of the framework.

Me, I hate the Magento model code, it’s the most convoluted code I’ve dealt with in a while.  I wish they would have used something like Doctrine.  But that is another post.

 
Magento Community Magento Community
Magento Community
Magento Community
 
ravip_magento
Jr. Member
 
Total Posts:  4
Joined:  2011-06-26
 

Hi LeeSaferite,

I have a problem and I think same kind of custom code.
Actually I have few zip codes where we can deliver our products so I want to display a error message and restrict to user for further processing on checkout page. I want this check on checkout page after entering zip code in shipping address.
Can you please suggest how I can do that kind of work in magento.

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top