× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Use Joomla's Database Class outside Joomla

<p>There might be certain situations where you wish to use Joomla's JDatabase <br> class outside of the joomla CMS in a seperate script, this can be done by <br> simply including this block of code in the top of your script.</p> <p> </p> <pre>// Initialize The Joomla Framework // ----------------------------------------------------------------------------------- define('_JEXEC', 1); // this is relative to the current script, so change this according to your environment define('JPATH_BASE', realpath(dirname(__FILE__) . '/..')); define('DS', DIRECTORY_SEPARATOR); // Require Joomla libraries require_once(JPATH_BASE . DS . 'includes' . DS . 'defines.php'); require_once(JPATH_BASE . DS . 'includes' . DS . 'framework.php'); require_once(JPATH_CONFIGURATION . DS . 'configuration.php'); require_once(JPATH_LIBRARIES . DS . 'joomla' . DS . 'database' . DS . 'database.php'); require_once(JPATH_LIBRARIES . DS . 'import.php'); // -----------------------------------------------------------------------------------</pre> <p> </p> <p>Now you should be able to use the JDatabase methods</p> <p> </p> <pre>// Get an instance of JDatabase $db = JFactory::getDbo(); // Get an instance of JDatabaseQuery $query = $db-&gt;getQuery(true); // Make a sql query $query-&gt;select("name")-&gt;from("#__users"); // Set the query $db-&gt;setQuery($query); // Fetch data $users = $db-&gt;loadColumn() // Print out data print_r($users);</pre> <p> </p>
comments powered by Disqus