Provide working code example of database operations

Hi there! The last few days I tried to get an app idea running but I failed. My PHP knowledge is limited, maybe the solution is quite simple for you (I bet ;)).

How can I perform operations on the database (as described in [1])?

In my app I want to let users of a certain group (not admin, more like moderators. This group shall not have full admin permissions) create their own groups. Theses users shall automatically have groupadmin rights for their self-created groups.
As I see it, this might be easily possible with a few SQL queries.

I generated an App template with This app, when installed runs the code in index.php.
The following code in index.php just outputs the navigation of the app, nothing else.

	namespace OCA\MyApp;
	use OCA\MyApp\Db\AuthorDao;
	$testobj=new AuthorDao();

In db/authordao.php I pasted the code from [1], changed the namespace to OCA\MyApp\Db and the query to SELECT * FROM *PREFIX*group_admin ’ . ‘WHERE uid = ?’;

What am I missing? Hope someone can help me, thanks.

PS: already tried to get the trick from other apps. But those are mostly really complex…


try like this:

use OCP\IDbConnection;
in construct:
... IDBConnection $db, ...
... $this->db = $db; ...

$SQL = "SELECT * FROM `*PREFIX*group_admin` WHERE `uid` = ?";
$stmt = $this->db->prepare($SQL);
$results = $stmt->fetchAll();


Hi Rello, thank you very much for your answer. I tried to implement the example again. I think I’m closer to the solution now (I get an error message…). As far as I understood I have to construct a new object of the DB-Class, right?

Where do you construct the object of your class CategoryController?
I do this here:

// This file: templates/content/index.php

namespace OCA\GroupManager;
use OCA\GroupManager\Db\DBOperations;

$testgroupman = new DBOperations();

My DBOperations Class looks like this:

// This file: db/dboperations.php

namespace OCA\GroupManager\Db;
use OCP\IDBConnection;

class DBOperations {

	/** @var IDBConnection */
	private $db;
	public function __construct(IDBConnection $db) {
		$this->db = $db;

	public function find($id) {
		$sql = 'SELECT * FROM `*PREFIX*group_admin` ' .
			'WHERE `uid` = ?';
		$stmt = $this->db->prepare($sql);
		$stmt->bindParam(1, $id, \PDO::PARAM_INT);

		$row = $stmt->fetch();

		return $row;

I get the error message, that I don’t pass the corrent argument to the constructor. But where can I get that “connection argument” from? The App should know it, since it’s running my nextcloud instance ^^

Internal Server Error

The server encountered an internal error and was unable to complete your request.

Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report.

More details can be found in the server log.

Technical details

	Remote Address: ip.ip.ip.ip
	Request ID: 4AJ3zPoubOphAbMkt3P4
	Type: TypeError
	Code: 0
	Message: Argument 1 passed to OCA\GroupManager\Db\DBOperations::__construct() must implement interface OCP\IDBConnection, none given, called in /var/www/html/foo/apps/groupmanager/templates/content/index.php on line 7
	File: /var/www/html/foo/apps/groupmanager/db/dboperations.php
	Line: 12


#0 /var/www/html/foo/apps/groupmanager/templates/content/index.php(7): OCA\GroupManager\Db\DBOperations->__construct()
#1 /var/www/html/foo/lib/private/Template/Base.php(176): include('/var/www/html/f...')
#2 /var/www/html/foo/lib/private/legacy/template.php(241): OC\Template\Base->load('/var/www/html/f...', NULL)
#3 /var/www/html/foo/apps/groupmanager/templates/index.php(14): OC_Template->inc('content/index')
#4 /var/www/html/foo/lib/private/Template/Base.php(176): include('/var/www/html/f...')
#5 /var/www/html/foo/lib/private/Template/Base.php(151): OC\Template\Base->load('/var/www/html/f...', NULL)
#6 /var/www/html/foo/lib/private/legacy/template.php(202): OC\Template\Base->fetchPage(NULL)
#7 /var/www/html/foo/lib/public/AppFramework/Http/TemplateResponse.php(157): OC_Template->fetchPage()
#8 /var/www/html/foo/lib/private/AppFramework/Http/Dispatcher.php(113): OCP\AppFramework\Http\TemplateResponse->render()
#9 /var/www/html/foo/lib/private/AppFramework/App.php(114): OC\AppFramework\Http\Dispatcher->dispatch(Object(OCA\GroupManager\Controller\PageController), 'index')
#10 /var/www/html/foo/lib/private/AppFramework/Routing/RouteActionHandler.php(47): OC\AppFramework\App::main('OCA\\GroupManage...', 'index', Object(OC\AppFramework\DependencyInjection\DIContainer), Array)
#11 [internal function]: OC\AppFramework\Routing\RouteActionHandler->__invoke(Array)
#12 /var/www/html/foo/lib/private/Route/Router.php(299): call_user_func(Object(OC\AppFramework\Routing\RouteActionHandler), Array)
#13 /var/www/html/foo/lib/base.php(1004): OC\Route\Router->match('/apps/groupmana...')
#14 /var/www/html/foo/index.php(48): OC::handleRequest()
#15 {main}