2

I am trying to practice Joomla database query. For learning purpose, I'd like to know in a blank PHP file under Joomla's root folder, which files do I need to include to be able to do

$db = JFactory::getDbo();
$query = $db->getQuery(true);

1 Answer 1

2

You will need to load the Joomla framework at the top of the PHP file, like so:

<?php

define('_JEXEC', 1);
define('JPATH_BASE', './'); // Assumes the PHP file is in the Joomla's root directory
require_once JPATH_BASE . 'includes/defines.php';
require_once JPATH_BASE . 'includes/framework.php';

You can then write your database query, for example, to load all the data from the #__users table:

<?php

define('_JEXEC', 1);
define('JPATH_BASE', './');
require_once JPATH_BASE . 'includes/defines.php';
require_once JPATH_BASE . 'includes/framework.php';

use Joomla\CMS\Factory;

$db = Factory::getDbo();
$db->setQuery(
    $db->getQuery(true)
        ->select('*')
        ->from($db->qn('#__users'))
);
$users = $db->loadObjectList();

var_dump($users);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.