MyBB Community Forums

Full Version: Run SQL Query within a plugin
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How are you guys running SQL Querys within a plugin ?

For example , how would this be run inside a plugin:
UPDATE `mybb_forums` SET `allowtratings` = '0' WHERE `allowtratings` = '1'

$update = array(
	"allowtratings" => 0
);
$db->update_query("forums", $update, "allowtratings = '1'");
Cheers matt Wink
Ok what im tring to do is , after i activate the plugin , i have a setting to in ACP "Yes/No"

If the user selects yes i want the SQL query to run and vice versa , what function should in use ?
Hook into admin_config_settings_change and store the current version of the setting in a global variable, then hook into admin_config_settings_change_commit and in that function pull the global var you grabbed and compare the new setting to the old one and if changed, run the query you want.
per your PM, here is code portion I was talking about

//group setting edits
$plugins->add_hook('admin_config_settings_change', 'thisplugin_get_current_setting');
$plugins->add_hook('admin_config_settings_change_commit', 'thisplugin_on_new_setting');

//specific/individual setting edit
$plugins->add_hook('admin_config_settings_edit', 'thisplugin_get_current_setting');
$plugins->add_hook('admin_config_settings_edit_commit', 

function thisplugin_get_current_setting()
{
	global $mybb, $thisplugin_before_commit_setting;
	
	$thisplugin_before_commit_setting = $mybb->settings['setting_to_capture'];

}

function thisplugin_on_new_setting()
{
	global $mybb, $thisplugin_before_commit_setting;
	
	if($thisplugin_before_commit_setting != $mybb->settings['setting_to_capture'])
	{
		//do your query here	
	}
}


i added the bit for single setting edits as well
sorry, i had addhook and not add_hook in teh above code. been corrected now