0

I have a custom php script that was written for a Joomla 3 site.

Of course on Joomla 4 it doesn't work.

I've been working on re writing the query statements to use prepared statements but even with the simplest dumbed down query I get a 500 error

Working off https://docs.joomla.org/J4.x:Selecting_data_using_JDatabase

<?php
// Connect to Joomla!
define( '_JEXEC', 1);
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', realpath(dirname(__FILE__) .'/' ) );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

//using new use statement instead of JFactory
use Joomla\CMS\Factory;

//copied from 
 $db = JFactory::getDbo();
 $query = $db->getQuery(true)
     ->select('*')
     ->from($db->quoteName('#__content'))
     ->setLimit('10');

$db->setQuery($query);
$result = $db->loadResult();
?>

What am I missing for a custom script to work on Joomla 4 ?

If I get that far I can maybe figure out the prepared statements for this: It's an inner join between custom fields table, content and categories


$query = $db->getQuery(true);
$query
->select($db->quoteName(array('v.item_id','cat.alias','c.title','c.alias','c.images','c.introtext','v.value'), array('item_id','category','title','alias','images','introtext','college')))
->from($db->quoteName('#__fields_values', 'v'))
->innerJoin($db->quoteName('#__content', 'c') . ' ON v.item_id = c.id')
->innerJoin($db->quoteName('#__categories', 'cat') . ' ON c.catid = cat.id')
->where($db->quoteName('v.field_id').'= :fieldValue')
->where($db->quoteName('c.state').'= :activeState')
->order($db->quoteName('c.title').'ASC');
->bind(':fieldValue', 5);
->bind(':activeState', 1);
   

$db->setQuery($query);
$articles = $db->loadObjectList();
5
  • Try declaring those integers in advance, then only pass variables to bind() methods. See the bottom of joomla.stackexchange.com/a/22712/12352 Apr 10 at 23:34
  • @mickmackusa that would help with my innerjoin query but doesn't help with my first part of what's the correct format for a custom php script with J4, because I can't even get the basic query to run.
    – hpgodwin
    Apr 11 at 0:55
  • Why are you passing a string value to setLimit()? ...hmm, to my surprise, the documentation demonstrates this too: docs.joomla.org/… I guess it's time for you to turn your error reporting all the way up and check the stacktrace / error logs. Apr 11 at 0:57
  • Your basic example appears to work, I've just copy and pasted it to a J4 site and it ran as is without any error and provided a correct value for $result when I var_dump'd it. Have you checked the server logs to determine what's causing the 500? It doesn't seem to be coming from Joomla Apr 11 at 7:40
  • thank you @MattGarrod! I don't have access to our server logs, hosting company, but I've emailed them about this and hopefully they will be able to provide the information needed. Knowing that the script does work in J4 is extremely helpful, so I can stop trying to rework it!
    – hpgodwin
    Apr 11 at 16:05

0

Your Answer

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

Browse other questions tagged or ask your own question.