Unable to access " owncloud.db " values in "upload.php"



I am working on ownCloud for my research. Actually I have inserted a new table in "owncloud.db". Now I want to access the values of this new inserted table in "upload.php" file.
For this I am using the select statement to access new table fields values. But I am unable to get the values.
How can I get the values from this new table from "upload.php" file?



In general it is not recommended to patch the existing code directly.

It is better to write an app that uses the ownCloud hooks to do things at upload time.

Now to answer your question, you can use the query builder to access values from a table, here is an example code: https://github.com/owncloud/core/blob/v9.1.4/lib/private/SystemTag/SystemTagManager.php#L102


Thanks for your quick response!

I still confused about the answer. Following screen shot will explain my complete question. I have table named as "oc_watermark". Now I want to access the filed "watermark" against "wmid". I have to write the select statement code in "upload.php" file.

I have searched that this can be like this:
select watermark from oc_watermark where wmid=1;

How can I use above select statement using "SystemTagManager.php"?:confused:


How about this:

$connection = \OC::$server->getDatabaseConnection();
$query = $connection->getQueryBuilder();
			->from('watermark') // no prefix needed
                        ->where($query->expr()->eq('wmid', $query->createNamedParameter($wmidValue)));
$result = $query->execute();

If that's too complicated you can look at simpler examples that do not use the query builder: https://github.com/owncloud/core/blob/master/apps/files_sharing/lib/ShareBackend/Folder.php#L71


Thanks for your precious time to answer me immediately!:relaxed:
Actually before I had selected an image from my desktop using following command, which works fine for me:

$watermark = imagecreatefromjpeg('/home/gcuf/Desktop/wm3.jpeg');

Now I am doing the same task, but now at this time, first of all, I have uploaded this picture in a newely inserted table. Now I am retrieving the same image from ownCloud table rather than from the desktop.

Now m using new code as you send me in last reply :heart_eyes:. Actually the purpose of the following script of code is to select the image from "oc_watermark" against id=1

$connection = \OC::$server->getDatabaseConnection();
$query = $connection->getQueryBuilder();
->from('oc_watermark') // no prefix needed
$result = $query->execute();
$watermark = imagecreatefromjpeg($result);
$target_image = imagecreatefromjpeg($files['tmp_name'][$i]);

But Now while selecting image from "oc_watermark" I m getting the following error? How can I get rid from the following error? :confused:



If you look closely at the error message it says "no such table 'oc_oc_watermark'".
In my snippet I added a comment that no prefix is needed so remove the "oc_" from the from() clause.


I have removed the prefix from the code. Now code looks like:

$connection = \OC::$server->getDatabaseConnection();
$query = $connection->getQueryBuilder();
->from('watermark') // no prefix needed
$result = $query->execute();
$watermark = imagecreatefromjpeg($result);
$target_image = imagecreatefromjpeg($files['tmp_name'][$i]);

But now m getting the following error:

Error uploading file "image1.jpeg": Internal Server Error

How to sort out this problem?



Did you look at the example snippet from the link ? https://github.com/owncloud/core/blob/v9.1.4/lib/private/SystemTag/SystemTagManager.php#L112

You need to retrieve the rows:

$result = $query->execute();
$row = $result->fetch(); // assuming only a single result here
$value = $row['watermark'];

After that I can't help you further as I'm not familiar with how images are returned from the database and how to make them work with imagecreatefromjpeg. You'll need to research this.


Thanks a lot Sir! :joy:

Finally I have achieved my desired goal using the following lines of code.

$connection = \OC::$server->getDatabaseConnection();
$sql = "SELECT watermark FROM oc_watermark WHERE wmid=2";
$sth = $connection->query($sql);
$row = $sth->fetch();
$image = $row['watermark'];
$watermark = imagecreatefromstring($image);
$target_image = imagecreatefromjpeg($files['tmp_name'][$i]);

Thanks a lot Sir! :sweat_smile: