How can I executive my own SQL queries withing Drupal? Please no references to the VIEWS module -- I get it; I know what it does. I want to query the database directly, either within the body of a "basic page" -- by setting the page content to "PHP Code" -- or by creating a stand-alone PHP script INCLUDE file which can be called at the top of a .tpl.php file. Attempts to do so have led to "undefined function" errors -- because apparently, attempting to make a SQL query this way, the system loses its access to Drupal's db_query, db_rewrite, etc. functions???
Does anyone know the best way for to create and execute my own SQL queries from within Drupal WITHOU having to create a module, or can this only be done by creating a module? OR Are there any modules out there that are designed for this very purpose: to allow developers to executive their own custom SQL queries and parse the results as we see fit?
Thanks.
Comments
Easy Dude
First there's two case you must consider
First case : if you are in one of drupal themes file or drupal file
Just call this function
$query ="select * ...." // your query
$result = db_query ($query);
$row = db_fetch_object($result);
Second case : if you created your own php file and want to connect to drupal database
just include first this two lines which connects to your drupal database
This can be done by creating
This can be done by creating a block, putting your php code in the block body and selecting php as the content. Be sure before you save your block, to limit it to displaying on one page only to prevent a error from wsod your entire site.
As you may already know, this is bad practice and can create a secure. Creating a module is the best way.
More info please
Guys, let me try posing the question this way:
Is there any reason why just inserting this code:
$sql="SELECT COUNT(*) FROM {node} WHERE type='article' AND status=1";
$total=db_result(db_query($sql));
at the top of my page.tpl.php file should lead to this error:
Call to undefined function db_result()
Why doesn't Drupal recognize its own database function? My page.tpl.php file is already embedded in the whole Drupal framework; when it is loaded, hasn't Drupal already loaded/included all of its bootstrap files?
Also...Easy Dude...what do you mean by "Drupal File" and "Drupal Themes File"?
MeInc....what do you mean by "wsod"
Thanks!
Check this
If you are using drupal 7 then this function is removed ..
Please habe a look to this link..
http://drupal.org/node/1063032
wsod
wsod = white screen of death