Page 1 of 1

DB access inside a usertag with PHP 7

Posted: Tue Oct 04, 2016 7:08 pm
by pengels
Hi all,

I have written a few usertags which needs access to the database of CMSMS. Up to PHP 5.5 I could easily use

mysql_query("TRUNCATE TABLE `buffertime`");

which works fine. With PHP 7 I need a second parameter:

mysqli_query($link,"TRUNCATE TABLE `buffertime`");

What is a correct, predefined variablename for $link containing the current database?

As a workaround I wrote a function open_db(), which will do the job using mysqli_connect and set $link = open_db().

But that cannot be the solution to the problem. Any hint?

Thanks in advance!

Regards Peter

Re: DB access inside a usertag with PHP 7

Posted: Wed Oct 05, 2016 4:50 am
by rotezecke
this works for me (on php 5.6.x or 7.0.x)
to access another database (not cmsms)

Code: Select all

$db = NewADOConnection('mysqli');
//assuming those $db_ vars are defined:
$db->Connect($db_host,$db_user,$db_pw,$db_db); 
to access cmsms i think this is all that's needed:

Code: Select all

$db = cmsms()->GetDb;
and then run your query:

Code: Select all

$query = "SELECT * FROM sometable";
$dbr = $db->Execute($query);
while( $dbr && !$dbr->EOF ) {
  print_r($dbr->fields);
  $dbr->MoveNext();
}
you may need to reconnect to cmsms with

Code: Select all

adodb_connect();

Re: DB access inside a usertag with PHP 7

Posted: Wed Oct 05, 2016 6:26 am
by chandra
It's not difficult to do that. $link means only connection ID.

You can get for current db with

Code: Select all

   $db = cmsms()->GetDb();
   $dbid = $db->connectionId;
, so this will work

Code: Select all

   $result = mysqli_query($dbid , 'select * from cms_content;');

Re: DB access inside a usertag with PHP 7

Posted: Wed Oct 05, 2016 1:33 pm
by pengels
Hi chandra,

thanks, that is exactly, what I was searching for! It works great.