DB access inside a usertag with PHP 7

The place to talk about things that are related to CMS Made simple, but don't fit anywhere else.
Post Reply
pengels
Forum Members
Forum Members
Posts: 34
Joined: Sat Dec 01, 2012 9:22 pm

DB access inside a usertag with PHP 7

Post 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
Regards from Germany, Peter
User avatar
rotezecke
Power Poster
Power Poster
Posts: 411
Joined: Fri Apr 18, 2008 9:34 pm

Re: DB access inside a usertag with PHP 7

Post 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();
chandra

Re: DB access inside a usertag with PHP 7

Post 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;');
pengels
Forum Members
Forum Members
Posts: 34
Joined: Sat Dec 01, 2012 9:22 pm

Re: DB access inside a usertag with PHP 7

Post by pengels »

Hi chandra,

thanks, that is exactly, what I was searching for! It works great.
Regards from Germany, Peter
Post Reply

Return to “The Lounge”