db_query From Online Manual

Revision as of 16:51, 8 October 2012 by Emanuele (talk | contribs)
Jump to: navigation, search


$smcFunc['db_query'] (identifier, query, values, connection)

Description: Works Similar to how db_query worked in 1.x versions.


  • identifier is used to identify specific query types in which it is necessary to replace portions of the query for compatability across all Database Management Systems supported by SMF. The query must be written for MYSQL. The db_query function will alter the query.
    • substring for the SQLite database, SUBSTRING will be replaced by SUBSTR
    • See the function smf_db_query in Subs-Db-{dbms}.php for the full list of replacements.
  • query the query to execute.
  • values is an array of values you are intending to use in the query.
  • connection the database connection.

Return: a query resource or false on error.


$result = $smcFunc['db_query']('', '
	SELECT poster_time
	FROM {db_prefix}messages
	WHERE id_msg = {int:id_msg}
	LIMIT 1',
		'id_msg' => $user_settings['id_msg_last_visit'],

Example with identifier 'substring':

$result = $smcFunc['db_query']('substring', '
	SELECT m.subject, 
		'. ($mylimit > 0 ? SUBSTRING(m.body, 1, '.$mylimit.') as body  : 'm.body'). ', 
		IFNULL(mem.real_name, m.poster_name) AS realName, m.poster_time as date, mem.avatar, mem.posts, mem.date_registered as dateRegistered,mem.last_login as lastLogin,
		IFNULL(a.id_attach, 0) AS ID_ATTACH, a.filename, a.attachment_type as attachmentType, t.id_board as category, b.name as category_name,
			'id_msg' => $user_settings['id_msg_last_visit'],