MyBB Community Forums

Full Version: mysql queries in a function in a plugin
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hai

This is probably very very easy, but I just can't find much documentation on it...

I would like to do this as simple as possible in a functions in a plugin...

$query=mysql_query('SELECT field1, field2 FROM mybb_table');
while($temparray=mysql_fetch_assoc($query)){
$array1[]=$temparray['field1'];
$array2[]=$temparray['field2'];
}

thanks in advance
You global the $db class and use queries like this:

$db->query();

Look at existing code for references as there are many $db-> functions.

$db->simple_select();
$db->delete_query();
$db->write_query();
$db->fetch_array();
$db->fetch_field();
$db->num_rows();

I think those are the most used.
Do you want that function in a plugin?

Well... create a php file: my_plugin.php

<?php

$plugins->add_hook("global_start", "my_plugin_function");

function my_plugin_function()
{
	global $mybb, $db;

	$q = $db->simple_select('table', 'field1, field2');

	while($temparray= $db->fetch_array($q))
	{
		$array1[] = $temparray['field1'];
		$array2[] = $temparray['field2'];
	}
}
?>

You can use $db->query('SELECT field1, field2 FROM mybb_table'), but simple_select is recommended.

Edit: that happens when I'm doing several things :p
$temparray should be $row in your example I believe.
Actually, simple_select wants table first then alternatively fields!

EDIT:
hmm, I will test what you guys posted now...
and yeah, $row or $temparray, same stuff, it's a temp array! ;o

2nd EDIT:
seems to work now! thanks a lot! =)