0

I'm developing a GIS mapping system and for that I am using pgSQL, however I am bounded to using mysql for the drupal install because we use Drupal 6 mulitsites using mysql already. I know I can get around this problem by hard coding using php, html to create my forms that submit to this. I know from Drupal Community Documentation this is not possible as standard. Is it possible via any other method to utilise the Form API and submit to an external DB in a different structure such as pgsql? or do i continue coding my forms by hand for each node?

The example below is exactly what I would like to do however it is clearly shown in the link above that this will not work.

// ... header of the settings.php file

$db_url = array (
"default" => "mysql://user:pass@host/db",
"second" => "pgsql://user:pass@host/db"
);

The code below is from the Form API getting started doc, however it doesnt specify the databse it is actually connecting to?

function test_myform_submit($form, &$form_state) {
 db_query("INSERT INTO {table} (name, log, hidden) VALUES ('%s', %d, '%s')",
   $form_state['values']['name'],
   $form_state['values']['access']['log'],
   $form_state['values']['hidden']);
drupal_set_message(t('Your form has been saved.'));
}
7
  • Form API does not save data. You do it in your own code. You can use mundane PHP PDO to connect to other databases if you need to. And why don't you switch to Drupal 7 for that one site if you need 7's features? After all 6 will be obsoleted and unsupported pretty soon anyway.
    – Mołot
    Commented Jul 24, 2013 at 10:06
  • @Mołot I would love to use D7, however I am restricted by the powers above, and the systems that have been developed to work on D6. Rumor has it we could be upgrading soon. So to Clarify, I create the forms using the API, and for each form I can select the database and the table it is connecting to, even if it is external to the drupal site and in a different format? I was under the impression the form API connects to the databases configured in the drupal settings file?7
    – Tom
    Commented Jul 24, 2013 at 10:39
  • @Mołot see edit at the bottom
    – Tom
    Commented Jul 24, 2013 at 10:47
  • @Tom If the form is cached it'll contact the Drupal db, yes. Your best bet will be to switch the db in your submit function, perform everything you need to on the external server, then switch back before handing the processing back to Drupal.
    – Clive
    Commented Jul 24, 2013 at 12:47
  • 2
    Both questions linked in "duplicate of" section have only Drupal 7 answers, and this question is about 6. If it's a duplicate, it is not a duplicate of questions linked above. Flagged for reopening.
    – Mołot
    Commented Jul 30, 2013 at 9:08

1 Answer 1

2

In Drupal 6 you cannot use more than one database engine with Drupal's functions. If you are using MySQL as Drupal storage, to connect to PostgreSQL you need to directly call PDO:

try {
  $dbh = new PDO('pgsql:host=localhost;port=5432;dbname=testdb;user=bruce;password=mypass');
} catch (PDOException $e) {
  watchdog("your module name",
           "PDO error: %error",
           array("%error" => $e->getMessage() ),
           WATCHDOG_ERROR);
}

Then use your handler just as you would in any non-Drupal PHP code, preferably with prepared statements.

2
  • So where I normally have my html forms, that I submit to a php file that connects. I will now, create forms using the drupal Form API, and then submit that form to an external php file with my connection details as your show, and my INSERT functions? Thanks
    – Tom
    Commented Jul 24, 2013 at 14:28
  • 1
    @Tom nope, you should build form as normal, and in submit callback you should create and use PDO connection. Or you can create it in your module and use where needed - details :) Point is, you should submit as you normally would, just some code inside that function needs to be changed.
    – Mołot
    Commented Jul 24, 2013 at 14:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.