accessing another database within c5 using the php block

Permalink
Hi,

I'm using an form app called MachForm that is also self-hosted php/mysql. It offers a php snippet to include the form on a php page.

When I use one of the PHP blocks on my c5 site to include the code I get errors.

Here they are (obviously not actually php):
mysql error: [1146: Table 'db84663_forms.CollectionVersionBlocks' doesn't exist] in EXECUTE("select Blocks.bID, CollectionVersionBlocks.arHandle from CollectionVersionBlocks inner join Blocks on (CollectionVersionBlocks.bID = Blocks.bID) inner join BlockTypes on (Blocks.btID = BlockTypes.btID) where CollectionVersionBlocks.cID = '95' and (CollectionVersionBlocks.cvID = '27' or CollectionVersionBlocks.cbIncludeAll=1) and CollectionVersionBlocks.arHandle = 'Sub-navigation' order by CollectionVersionBlocks.cbDisplayOrder asc")
Fatal error: Uncaught exception 'ADODB_Exception' with message 'mysql error: [1146: Table 'db84663_forms.Logs' doesn't exist] in EXECUTE("insert into Logs (logType, logText, logIsInternal) values ('exceptions', 'Exception Occurred: mysql error: [1146: Table \'db84663_forms.CollectionVersionBlocks\' doesn\'t exist] in EXECUTE("select Blocks.bID, CollectionVersionBlocks.arHandle from CollectionVersionBlocks inner join Blocks on (CollectionVersionBlocks.bID = Blocks.bID) inner join BlockTypes on (Blocks.btID = BlockTypes.btID) where CollectionVersionBlocks.cID = \'95\' and (CollectionVersionBlocks.cvID = \'27\' or CollectionVersionBlocks.cbIncludeAll=1) and CollectionVersionBlocks.arHandle = \'Sub-navigation\' order by CollectionVersionBlocks.cbDisplayOrder asc")\n\n#0 /nfs/c04/h05/mnt/84663/domains/saphireeventgroup.com/html/concrete/libraries/3rdparty/adodb/adodb.inc.php(1042): adodb_throw(\'mysql\', \'EXECUTE\', 1146, \'Table \'db84663_...\', \'select Blocks.b...\', false, Object(ADODB_mysql))\n#1 /nfs/c04/h05/m in /nfs/c04/h05/mnt/84663/domains/saphireeventgroup.com/html/concrete/libraries/3rdparty/adodb/adodb-exceptions.inc.php on line 78


This can be seen at:http://www.saphireeventgroup.com/index.php?cID=95...

It appears that there is a mix-up on which database to access, so an error is generated ("db84663_forms" is the form app database).

Is there a way to make this work? I have the php code for the form app and can make modifications if necessary.

Thank you!

kirkroberts
 
Mnkras replied on at Permalink Reply
Mnkras
have you tried it in a singlepage? make a singlepage with the snippet in the appropriate place, (look at the login single page for an example on where to put it)
okhayat replied on at Permalink Reply
okhayat
The best solution to access another database (I found) is to prefix the table name with the database name.
So, instead of using '... from table' make it ' ... from db.table'.
As long as the user/password are the same, this should work.
kirkroberts replied on at Permalink Reply
kirkroberts
Tried putting the code into a single page but still got an error. It seems that c5 is trying to read tables in the other database. Why might this be? Is a key variable getting overwritten partway into the page?

I know nothing about mysql commands, so I'm not sure how to go about modifying how this other app accesses the second database.

How/where would I go about prepending the db name to the table?

Would it be here? (seems unlikely)
function connect_db(){
      $link = mysql_connect(MF_DB_HOST, MF_DB_USER, MF_DB_PASSWORD) or die('Could not connect: ' . mysql_error());
      mysql_select_db(MF_DB_NAME) or die('Could not select database');
      mysql_query("SET NAMES utf8");
      return $link;
   }

(I prepended the DB variables with "MF_" in hope that would keep them from interfering with c5.)

Or here (for example)? (seems more likely)
$query    = "select 
                   form_name,
                   form_description,
                   form_redirect,
                   form_success_message,
                   form_password,
                   form_unique_ip,
                   form_frame_height,
                   form_has_css,
                   form_active,
                   form_captcha,
                   form_review
                 from 
                     ap_forms 
                where


For now I'm having to use an iframe to keep the two apps from interfering with each other.

Maybe there is a simpler way to shield one app from the other?
ScottC replied on at Permalink Reply
ScottC
well you should be able to use the Loader::db() but pass in the parameters in the correct sequence. All of them are optional as they default to the config, app or what have you defaults which is why you can instantiate it without any parameters, but the location password etc are available to be overridden over the default values.
kirkroberts replied on at Permalink Reply
kirkroberts
Thanks everyone for your replies. The client is happy enough with using an iframe to house the form (which avoids the database conflict) and I need to move on. I'll probably be revisiting this at a later time, though.