Question

I like to read an article by id from Joomla 2.5. As I'm not inside the framework I've to include it first. I found also some samples how to get an article by id than but it ever fails ... This is the sample I'm trying:

define( '_JEXEC', 1 );
define( '_VALID_MOS', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__)));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
echo JPATH_BASE .DS.'includes'.DS.'framework.php';
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
echo  $mainframe->getCfg('sitename');

$articleId = JRequest::getInt('Itemid');
$db =& JFactory::getDBO();

$sql = "SELECT fulltext FROM #__content WHERE id = 260"; //.intval($articleId);
$db->setQuery($sql);
$fullArticle = $db->loadResult();

Article ID 260 is available but it ever return null ...

When I trace it $cursor in loadResult() is every null:

public function loadResult()
{
    // Initialise variables.
    $ret = null;

    // Execute the query and get the result set cursor.
    if (!($cursor = $this->execute()))
    {
        return null;
    }
     ...

Can somebody help please?

Thanks Andre

Was it helpful?

Solution

There are a few things you don't need in your script and I have changed it to Joomla 2.5 coding standards:

define('_JEXEC', 1);
define('JPATH_BASE', realpath(dirname(__FILE__)));
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );
require_once ( JPATH_BASE .'/libraries/joomla/factory.php' );

$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('introtext')
 ->from('#__content')
 ->where('id = 260');
$db->setQuery($query);

$fullArticle = $db->loadResult();

echo $fullArticle;

Hope this helps

OTHER TIPS

Try this SQL query, it should work now!

$sql  = 'SELECT `fulltext` FROM `#__content` WHERE `id` = 260';

What Lodder has said is exactly the same, only better structured than your query.

So you can code in that style if it suites you. In his way of coding it would become:

$sql = $db->getQuery(true);
$sql->select('fulltext')
 ->from('#__content')
 ->where('id = 260');
$db->setQuery($sql);

Easy to read and modify this way, isn't it? :)

Also, you can fetch the content from `introtext` as the `fulltext` is null at times.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top