Quantcast
Channel: Joomla! Forum - community, help and support
Viewing all articles
Browse latest Browse all 1690

Joomla! 5.x Coding • select query with loadAssoc not returning array

$
0
0
Hi-

When I used:

Code:

use Joomla\CMS\Factory;$user = Factory::getUser();$db = Factory::getContainer()->get('DatabaseDriver');$userId  = $user->get('id');$db->setQuery('SELECT u.name, u.email, u.username, p.*, s.sig_form, s.year FROM (`#__users` u JOIN `#__jsn_users` p ON u.id = p.id) LEFT JOIN `#__jsn_sig_form` s ON u.id = s.user_id AND s.year = YEAR(CURDATE()) WHERE u.id = '.$userId);$results = $db->loadAssoc();
I get a result.

However, that is not the correct way to do things, so I updated my code to:

Code:

use Joomla\CMS\Factory;$user = Factory::getUser();$db = Factory::getContainer()->get('DatabaseDriver');$userId  = $user->get('id');$year = date('Y');$query = $db->getQuery(true);    $query       ->select('u.name', 'u.email', 'u.username', 'p.*', 's.sig_form', 's.year')       ->from($db->quoteName('#__users', 'u'))       ->join('INNER', $db->quoteName('#__jsn_users', 'p').' ON ('.$db->quoteName('u.id').' = '.$db->quoteName('p.id').')')       ->join('LEFT', $db->quoteName('#__jsn_sig_form', 's').' ON ('.$db->quoteName('u.id').' = '.$db->quoteName('s.user_id').' AND '.$db->quoteName('s.year').' = :year)')       ->where($db->quoteName('u.id').' = :userid')       ->bind(':userid', $userId)       ->bind(':year', $year);$db->setQuery($query);$results = $db->loadAssoc();
But I do not get a result and there is no error. What am I doing wrong in the second example? All I can think of is that the joins are not behaving as expected, so the result ends up NULL, but I have no idea how I would rewrite it so it works correctly. Any help is much appreciated.

Statistics: Posted by kgriffin — Tue Aug 13, 2024 5:00 pm



Viewing all articles
Browse latest Browse all 1690

Trending Articles





<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>