x
 
Create New Page
 View Previous Changes    ( Last updated by Lisa Wess )

SQL query with pure PHP

How to perform an SQL query without resorting to the {exp:query} module

There are times when using the built-in {exp:query} module is not an option or desirable. In this cases, PHP can be used:

<?php global $PREFS

$db mysql_connect// Handle the connection to the database. 
$PREFS->core_ini['db_hostname']// hostname, 
$PREFS->core_ini['db_username']// username and 
$PREFS->core_ini['db_password']); // password are automatically pulled 
mysql_select_db($PREFS->core_ini['db_name']); // from EE's config.php 

// Adjust your query as needed. In this example, the ids of all open entries from 
// weblog_id 1 are queried: 

$query "SELECT entry_id AS entry FROM exp_weblog_titles WHERE (weblog_id = '1' AND status = 'open') 
ORDER BY entry_id"


// the result is stored in $result, the number of returned rows in $num: 
$num mysql_numrows($result mysql_query($query,$db)); 

mysql_close(); // close the connection to the database 

// now we loop through the results and actually do some stuff: 

for ($i 0$i <= $num-1$i++) 

$id 
mysql_result($result,$i,"entry"); 
print 
$id '<br />'} ?> 

Note: This is a kind of “last resort” solution. It is usually much more desirable to use the built-in Queries module.

Category:Templates Category:Queries Category:Core

Category:EE1

Categories: