diff --git a/phpBB/includes/acp/acp_bbcodes.php b/phpBB/includes/acp/acp_bbcodes.php
index d806dbc801..e42e2d0d96 100644
--- a/phpBB/includes/acp/acp_bbcodes.php
+++ b/phpBB/includes/acp/acp_bbcodes.php
@@ -342,24 +342,26 @@ class acp_bbcodes
 			'U_ACTION'		=> $this->u_action . '&action=add',
 		);
 
-		$sql = 'SELECT *
-			FROM ' . BBCODES_TABLE . '
-			ORDER BY bbcode_tag';
+		$sql_ary = array(
+			'SELECT'	=> 'b.*',
+			'FROM'		=> array(BBCODES_TABLE => 'b'),
+			'ORDER_BY'	=> 'b.bbcode_tag',
+		);
 
 		/**
 		*  Modify bbcode template data before we display the form
 		*
 		* @event core.acp_bbcodes_display_form
 		* @var	string	action			Type of the action: modify|create
-		* @var	string	sql				SQL statement to get bbcode data
+		* @var	string	sql_ary			The SQL array to get custom bbcode data
 		* @var	array	template_data	Array with form template data
 		* @var	object	this_u_action	$this->u_action object
 		* @since 3.1-A3
 		*/
-		$vars = array('action', 'sql', 'template_data', 'this_u_action');
+		$vars = array('action', 'sql_ary', 'template_data', 'this_u_action');
 		extract($phpbb_dispatcher->trigger_event('core.acp_bbcodes_display_form', compact($vars)));
 
-		$result = $db->sql_query($sql);
+		$result = $db->sql_query($db->sql_build_query('SELECT', $sql_ary));
 
 		$template->assign_vars($template_data);
 
@@ -387,7 +389,7 @@ class acp_bbcodes
 
 		}
 		$db->sql_freeresult($result);
-		
+
 		$this->u_action = $this_u_action;
 	}
 
diff --git a/phpBB/includes/functions_display.php b/phpBB/includes/functions_display.php
index e0bf6786e1..0525f8133c 100644
--- a/phpBB/includes/functions_display.php
+++ b/phpBB/includes/functions_display.php
@@ -928,22 +928,24 @@ function display_custom_bbcodes()
 	// Start counting from 22 for the bbcode ids (every bbcode takes two ids - opening/closing)
 	$num_predefined_bbcodes = 22;
 
-	$sql = 'SELECT bbcode_id, bbcode_tag, bbcode_helpline
-		FROM ' . BBCODES_TABLE . '
-		WHERE display_on_posting = 1
-		ORDER BY bbcode_tag';
+	$sql_ary = array(
+		'SELECT'	=> 'b.bbcode_id, b.bbcode_tag, b.bbcode_helpline',
+		'FROM'		=> array(BBCODES_TABLE => 'b'),
+		'WHERE'		=> 'b.display_on_posting = 1',
+		'ORDER_BY'	=> 'b.bbcode_tag',
+	);
 
 	/**
-	* Modify the SQL statement retrieving the custom bbcodes
+	* Event to modify the SQL query before custom bbcode data is queried
 	*
 	* @event core.display_custom_bbcodes_modify_sql
-	* @var	string	sql		SQL SELECT statement
+	* @var	array	sql_ary	The SQL array to get the bbcode data
 	* @since 3.1-A3
 	*/
-	$vars = array('sql');
+	$vars = array('sql_ary');
 	extract($phpbb_dispatcher->trigger_event('core.display_custom_bbcodes_modify_sql', compact($vars)));
 
-	$result = $db->sql_query($sql);
+	$result = $db->sql_query($db->sql_build_query('SELECT', $sql_ary));
 
 	$i = 0;
 	while ($row = $db->sql_fetchrow($result))
@@ -963,7 +965,7 @@ function display_custom_bbcodes()
 		);
 
 		/**
-		* Modify the template data block of a bbcode
+		* Event to modify the template data block of a custom bbcode
 		*
 		* This event is triggered once per bbcode
 		*