XOOPS has a database abstraction layer for you to use, when accessing the database.
The database object can be retrieved in two ways:
a) using the $xoopsDB instance - if you are in a function or class method, you will need to declare it global first with
global $xoopsDB;
b) calling a static getInstance() method on the Database class
$xoopsDB =& XoopsDatabaseFactory::getDatabaseConnection();
After that is done, you can use the database object to query the database
//Any statement goes in the query - SELECT, UPDATE, INSERT etc.
$result = $xoopsDB->query('SELECT * FROM [...] ');
// if it is a SELECT statement, $result will now be a resultset so let's loop through it
while ($row = $xoopsDB->fetchArray($result)) {
$variable = $row['index'];
$another_variable = $row['another_index'];
}
If the query is not a SELECT statement, $result will be true or false, depending on whether the SQL query encountered errors or not.
See also this
FAQ here
My list of DB queries
Here are some of my db queries to access the XOOPS database.
// To delete rom from table
$query = "Delete from ".$xoopsDB->prefix("xoops_table")." where id='$id'";
// To insert a row into the table
$sql = "INSERT INTO ".$xoopsDB->prefix('xoops_table');
$sql .= " ( xuser, emailname ) VALUES ";
$sql .= " ( '$user', '$userwebname' )";
if ( ! $xoopsDB->query($sql) )
{
echo( $xoopsDB->error." : ".$xoopsDB->errno );
}
//To update a row
$query = "Update ".$xoopsDB->prefix("xoops_table")." smtpuname = '$smtpuname', smtppasswd = '$smtppasswd' where id='$id' ";
//Select from row
$query = 'SELECT field1, field2 FROM ' . $xoopsDB->prefix('tablename') . ' WHERE searchfield1 =1';
//Query database
$query = "select * FROM ".$xoopsDB->prefix("xoops_table")." where uid = $userid";
$results=$xoopsDB->query($query,$options[0],0);
//Compare 2 groups through a user uid. 1 user stored in db, other is current. I have to thank Mithrandir for giving me this code.
$db_uid = $row['uid'];
$userid = $xoopsUser->uid();
if ($userid != $db_uid) {
$member_handler =& xoops_gethandler('member');
$db_groups = $member_handler->getGroupsByUser($db_uid);
$current_groups = $xoopsUser->getGroups();
$common_groups = array_intersect($current_groups, $db_groups);
}
if ($userid==$db_uid || !empty($common_groups)){
//do something
}