Forums

User ID unique call

MrOcco 06 Jul, 2013
Hi Guys, please help. I have a form where only registered users can access. Now in the first part of the form I have a php action pulling data from the DB until I get to the following:

<?php
$db = JFactory::getDBO();
$db->setQuery('SELECT cf_deladdress FROM #__uu_users WHERE user_id=$userid');
$databaseTitle = $db->loadResult(); 
?>
<?php echo $databaseTitle; ?> 


It will return nothing because it cant find the current userid of the logged in user, however if I change the $userid to the id of a logged in member the results display correctly. How do I dynamically use the above code to retrieve user id and input it after the WHERE call?
Thanks
Please bare in mind Im new to php code.
GreyHead 06 Jul, 2013
Hi mrocco,

Please try:
<?php
$user =& JFactory::getUser();
$db =& JFactory::getDBO();
$query = "
SELECT `cf_deladdress` 
  FROM `#__uu_users` 
  WHERE `user_id` = {$user->id} ;
";
$db->setQuery($query);
$databaseTitle = $db->loadResult();
echo $databaseTitle;
?>


Bob
MrOcco 06 Jul, 2013
Hi Bob, thanks I tried that, still no return.
Anything else I can try.
I have got the GH load user info addon aswell, could that be causing it not to display?

Joomla 2.5.11
Chronoforms v4

PS if I use your code and replace {$user-id} with the actual user ID number eg 949 it works perfectly.

If I use the following it displays the ID correctly:

<?php
$user =& JFactory::getUser();
 
if (!$user->guest) {
  echo 'You are logged in as:<br />';
  echo 'User name: ' . $user->username . '<br />';
  echo 'Real name: ' . $user->name . '<br />';
  echo 'User ID  : ' . $user->id . '<br />';
}
?>
GreyHead 10 Jul, 2013
Hi mrocco,

The query looks OK to me - try debugging it with a few 'echo' statements to see exactly what is happening in your code.

The User Info [GH] action shouldn't cause a problem; try removing it to check though.

Bob
MrOcco 10 Jul, 2013
Hi Bob, it works

The idiot that I am put an extra , in by mistake, was pulling out my hair.

Your a genius!!!!!

Thanks
This topic is locked and no more replies can be posted.