MyBB Community Forums

Full Version: Small bug with a custom piece of code - throws a MySQL error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi peeps Smile

I have this piece of code here that generates a neat 'n' tidy list of posts by the specific user:

elseif($mybb->input['action'] == "getuserposts")
{
	$where_sql = "uid='".intval($mybb->user['uid'])."'";

	$unsearchforums = get_unsearchable_forums();
	if($unsearchforums)
	{
		$where_sql .= " AND fid NOT IN ($unsearchforums)";
	}
	$inactiveforums = get_inactive_forums();
	if($inactiveforums)
	{
		$where_sql .= " AND fid NOT IN ($inactiveforums)";
	}

	$tids = '';
	$comma = '';
	$query = $db->query("SELECT DISTINCT tid FROM ".TABLE_PREFIX."posts WHERE {$where_sql}");
	while($tid = $db->fetch_field($query, "tid"))
	{
			$tids .= $comma.$tid;
			$comma = ',';
	}

	$sid = md5(uniqid(microtime(), 1));
	$searcharray = array(
		"sid" => $db->escape_string($sid),
		"uid" => $mybb->user['uid'],
		"dateline" => TIME_NOW,
		"ipaddress" => $db->escape_string($session->ipaddress),
		"threads" => $db->escape_string($tids),
		"resulttype" => "threads",
	);
	$plugins->run_hooks("search_do_search_process");
	$db->insert_query("searchlog", $searcharray);
	redirect("search.php?action=results&sid=".$sid, $lang->redirect_searchresults);
}

However, if the user has no posts, instead of writing a nice "Sorry, but no results were returned using the query information you provided. Please redefine your search terms and try again." message like the finduserthreads action that comes with MyBB:

elseif($mybb->input['action'] == "finduserthreads")
{
	$where_sql = "t.uid='".intval($mybb->input['uid'])."'";

	$unsearchforums = get_unsearchable_forums();
	if($unsearchforums)
	{
		$where_sql .= " AND t.fid NOT IN ($unsearchforums)";
	}
	$inactiveforums = get_inactive_forums();
	if($inactiveforums)
	{
		$where_sql .= " AND t.fid NOT IN ($inactiveforums)";
	}
	
	$permsql = "";
	$onlyusfids = array();

	// Check group permissions if we can't view threads not started by us
	$group_permissions = forum_permissions();
	foreach($group_permissions as $fid => $forum_permissions)
	{
		if($forum_permissions['canonlyviewownthreads'] == 1)
		{
			$onlyusfids[] = $fid;
		}
	}
	if(!empty($onlyusfids))
	{
		$where_sql .= "AND ((t.fid IN(".implode(',', $onlyusfids).") AND t.uid='{$mybb->user['uid']}') OR t.fid NOT IN(".implode(',', $onlyusfids)."))";
	}

	$sid = md5(uniqid(microtime(), 1));
	$searcharray = array(
		"sid" => $db->escape_string($sid),
		"uid" => $mybb->user['uid'],
		"dateline" => TIME_NOW,
		"ipaddress" => $db->escape_string($session->ipaddress),
		"threads" => '',
		"posts" => '',
		"resulttype" => "threads",
		"querycache" => $db->escape_string($where_sql),
		"keywords" => ''
	);
	$plugins->run_hooks("search_do_search_process");
	$db->insert_query("searchlog", $searcharray);
	redirect("search.php?action=results&sid=".$sid, $lang->redirect_searchresults);
}

, it simply throws this error:

http://i.imgur.com/8ClBpHw.jpg

Any erm, thoughts on this?

I'm new to all this so am confused...

But thank you, and for the most part my forum is working super great! ^_^

Wave