I've got a file with 2 functions. One function has a db_select query. At one point in the other function, it calls the first function, but the query won't run. However, if I pull the query out and paste it directly into the second function, it runs fine. Is there something special I need to do inside of the first query to make it recognizable to the second query when called?

Comments

nevets’s picture

Seeing the code would make it easier for people to provide help (please remember to place code between <code> and </code> tags).

darrindickey’s picture

function matchup_contents() {
	$subquery = db_select('field_data_field_table')
		->fields('field_data_field_passions', array('field_table_value'))
		->condition('field_data_field_table.entity_id', $user->uid, '=');
					
	$query = db_select('field_data_field_table', 't')
		->fields('t', array('entity_id'))
		->distinct()
		->condition('field_table_value', $subquery, 'IN')
		->condition('entity_id', $user->uid, '<>')
		->orderBy('entity_id', 'ASC');
	$result = $query->execute();
}

function matchup_block_view($delta = '') {
	$block = array();
 	switch($delta){
  		case 'matchup':
  		global $user;
   			$block['subject'] = t('Users with similar interests');
   				if(user_access('access content')){
					
			    	
			$result = matchup_contents();
			
			$items = array();
			
			foreach ($result as $num) {
				$items[] = array($num->entity_id);	
			}
					
			$subquery2 = db_select('field_data_field_table')
			          ->fields('field_data_field_table', array('field_table_value'))
					  ->condition('field_data_field_table.entity_id', $user->uid, '=');
					  
			$query2 = db_select('field_data_field_table', 't')
				   ->fields('t', array ('field_table_value'))
				   ->condition('field_table_value', $subquery2, 'IN')
				   ->condition('entity_id', $user->uid, '<>')
				   ->condition('entity_id', array($num->entity_id), '=');
				   
		    $result2 = $query2->execute();
	
					$items2 = array();
		
					foreach ($result2 as $row){
						$items2[] = array($row->field_table_value);	
	  	 			}
					
			if (empty($items)) { //No content in the last week.
          		$block['content'] = t('No matches available.');  
        	} else {
          		//Pass data through theme function.
          		$block['content'] = t($num->entity_id) . theme('item_list', array('items' => $items2));
        	} 
  	}
 }
 return $block;
 }
nevets’s picture

A problem I notice is that matchup_contents() uses $user but does not declare it as global.

darrindickey’s picture

Alright, I added:

global $user;

to the function. It returns a response of NULL.

nevets’s picture

How about adding

   return $result;

as the last line in the function.

darrindickey’s picture

That was it! Thanks!!