Search code examples
phpjoomlaresultset

Get single value from a single row of a Joomla database query result


I am trying to pull the value from the loadResultArray call below:

$query = 'SELECT cb_featuredvideo FROM #__comprofiler WHERE user_id = '.$id.'';

// Reset the query using our newly populated query object.
$db->setQuery($query);
$youtube = $db->loadResultArray();
$link = $youtube[0];
// Load the results as a list of stdClass objects.
$results = $db->loadObjectList();

The problem is when I check the value for $link it is coming up as null. If I print the array from $results I get the expected output:

Array ( [0] => stdClass Object ( [cb_featuredvideo] => http://youtu.be/ytJCZnG3k9k ) ) 

Why isn't the $link value showing the youtube link?


Solution

  • What about loadResult() ?

    According to http://docs.joomla.org/Selecting_data_using_JDatabase :

    Use loadResult() when you expect just a single value back from your database query.

    $db = JFactory::getDbo();
    $query = $db->getQuery(true);
    $query->select('field_name');
    $query->from($db->quoteName('#__my_table'));
    $query->where($db->quoteName('some_name')." = ".$db->quote($some_value));
    
    $db->setQuery($query);
    $result = $db->loadResult();