External Database Query

Events happening in the community are now at Drupal community events on www.drupal.org.
sdudenhofer's picture

Wondering if anyone has any experience querying an external database and displaying the results in a node.

Actually both querying and inserting into a database....

I know I could build pages with php code, but I would rather use a module (if its available) or a drupal standard way....

Thanks!!

Comments

Haven't had need to do this

dunkoh's picture

Haven't had need to do this type of thing yet - a quick search didn't result in any modules (I may have missed it though)
But here are two pages that may assist you if there aren't any modules available....

http://drupal.org/node/18429

http://drupal.org/node/265367

I do this a lot, and it's

Jamie Holly's picture

I do this a lot, and it's rather simple. Set the configuration in your settings.php file for the database:

$db_url[{a name for the connection}]="{drupal styled connection string}";

Then in your module before you query the database just do a:

db_set_active({the name from above});

After the query return Drupal back the the default database:

db_set_active();


HollyIT - Grab the Netbeans Drupal Development Tool at GitHub.

set active is site wide no?

salientknight's picture

set active is site wide no? so any query that tries to run before you set it back will fail no?

apparently views 2 also has

dunkoh's picture

apparently views 2 also has ability to connect to an external db, but not through UI

mag2000's picture

as you mention there is no ui for connect the external database in view.

I know if you set the database in setting.php. and then use php code to fetch the required field.

i have the content type product and capacity in mysql. Product have pk prdcode and capacity have fk c_prdcode. I have created the content type product and capacity in drupal. then i populate the node data as well. But no relation between prdcode and C_prodcode created in drupal. I know if i create the nodereference field in content type and define the relation between product content type and capacity content type will give me the relationship. But i do not want to enter the data again as i have thousand of row in product and capacity in mysql table.

I want to display data in view and do not want any data enter in drupal. That's why i want show the data through view. the view fields will be the column of both the table example. product code,product name, capacity,since....

I want to know how can i give the external query to view and how i map the field of view with external database column which will return due to query.

If you provide me the php code and where i insert in view...thanks

thanks for help

I h

Have you looked at table

bigjim's picture

Have you looked at table wizard?

http://drupal.org/node/452374

Yeah, and Views 3 for external data (D7)

mradcliffe's picture

Yeah, table wizard is what you're looking for. Also, Views 3 (and even more so with Drupal 7 Views 3) will make it easier to display external data.

i use drupal 6

mag2000's picture

i do not have option to use drupal 7 right now

It is interesting

chrispooh's picture

Hello, it is interesting for me to use external database with views 3. Where can ia find information about this?

With Table Wizard and Data

bigjim's picture

With Table Wizard and Data Modules pretty much dying it's not terrible simple really.

It's easy to expose the 3rd party db by adding the url in the settings.php file db_url array. What gets tricky is getting views to recognize the db and telling views how to manage its tables/fields (ie Views handlers). Basically you're gonna hav to build the bridge using hook_views_data() and maybe build custom handlers depending on your needs

Views 3 is available for D6

bigjim's picture

Views 3 is available for D6 (it's alpha but it is stable), but as Matt said D7 is even better.

Unable to to do from table wizard rewrite my question

mag2000's picture

I tried table wizard but does not allow relation between two external tables. Data module create the table in drupal i do not want to create the table as well.

I have the database external name : Mydbase

Following table are there.

Tables Fields

Company maincode, subcode, company name

Product product code, product name

Capacity maincode, subcode, product code, capacity, since.

one to many relation of company and capacity

one to many relation of product and capacity.

want to display in view

maincode, subcode,product code, company name,product name, capacity,since.

want to expose the product code filter as well

I have setup the setting.php db_url of external database.

Then i create the form in page for select the company. Once the user select the company i store the company code in session variable for reference. This variable i will use for fetch information in view. I will enter its value in default argument php code and view will provide me the capacity information of selected company. This page auto redirect the user to view page of capacity. by using drupal_got0() function. the code of the page is attached below.. In code i have one problem.I can not store the company name in variable,only able to fetch the information maincode and subcode. How the fetch the display value of drop down instead of value tag value...

I do not know how to use view for external database without the content type define in drupal, and do not have any cck field as well. If i can i want to expose the product code as well in the view.so user can select and view one product of select company.

+++++++++++++++++ code start +++++++++++++++++++++

<?php
if(isset($_SESSION['ses_maincode'])){
echo "Company Main code Selected = " . $_SESSION['ses_maincode'] . "";
echo "Company Sub code Selected = " . $_SESSION['ses_subcode'] . "";
echo "
";
}
$output .= drupal_get_form('formexample_testform');

echo $output;

function formexample_testform() {
$myvalue=array();
$count = 0;
db_set_active('mydb');
$result = db_query('SELECT maincode,subcode,cname FROM {company}');
while ($row = db_fetch_array($result)) {
$arrayvalue=$row[maincode] . '+' . $row[subcode];
$myvalue[$arrayvalue]=$row['cname'];
$count = $count +1;
}
db_set_active('default');
$form['select_company'] = array(
'#title' => t('Select Company'),
'#type' => 'select',
'#description' => t('Pick the company '),
'#options' => $myvalue,
'#multiple' => FALSE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit')
);
return $form;
}

function formexample_testform_submit($form_id, $form_state) {
$name=$form_state['values']['select_company'];
drupal_set_message(t(' You have select the company %name',
array('%name' => $name)));

$mystring = $form_state['values']['select_company'];
$pos = strrpos($mystring, "+");
$endpoint = strlen($mystring) - $pos;
$_SESSION['ses_maincode']=substr($mystring,0,$pos);
$_SESSION['ses_subcode']=substr($mystring,$pos+1,$endpoint);

$urlstring='node/7';
/** . $_SESSION['ses_maincode']; */
drupal_goto($urlstring);

}
?>

+++++ code end here

Connect

chrispooh's picture

I tried to connect, but not works.

This is my settings.php:

$databases['dbname']['dbname'] = array(
'database' => 'dbname',
'username' => 'dbusername',
'password' => 'dbuserpw',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
);

Is it correct?

@chrispooh To be honest I

bigjim's picture

@chrispooh

To be honest I haven't had to do this in D7 yet, maybe this would help?

http://drupal.org/node/18429

Try with no custom ['dbname']['dbname']

qpro's picture

I think the problem is using custom names for ['dbname']['dbname'], try with ['default']['slave'][] or ['extra']['default'] as is indicated in http://drupal.org/node/310071

It is good, i found this

chrispooh's picture

It is good, i found this article before, but the big question: how can i use this database connection?
I made a module to use a hook_views with this database, but the view always want to use the default database :(

I think what your looking for

bigjim's picture

I think what your looking for is in here, http://drupal.org/node/989542, but baically once your settings.php is set up you set the db in the hook_views_data() like this

assuming db is labeled exttable

$data['exttable']['table']['base'] = array(
'field' => 'id',
'title' => 'External data',
'weight' => 10,
'database' => 'extdb',
);

dev module Feeds SQL

qpro's picture

I have just found this module to query a external database: http://drupal.org/project/feeds_sql

kriskhaira's picture

You can set up multiple databases within Drupal using the $db_url variable in settings.php. You could call the Drupal one "default" and the other one "external".
http://drupal.org/node/18429

You can then query this database using db_query() and db_fetch_object(). The example in the following link is for Drupal 5 but should work in 6 and 7 with slight modifications:
http://drupal.org/node/139575

You can create $db_url on the fly

sudarshan_86's picture

global $db_url,$user;
$connstr = "mysql://".$mysqluser.":".$pass."@localhost/$dbname";
$default = $db_url;
$db_url['default'] = $default;//keep the default
$db_url[$user->name] = $connstr;//add new
db_set_active($user->name);//set it to new

andrés chandía's picture

Hi there,
I have created a basic page coded in php where I put the query to an external database, collation in utf8, the drupal database is also utf8 collation.
Well at the resulting page characters are not recognized, for instalce:
as�

If I put the same code at a php file and I browse it, i have:
así (i with accent, as it is at the database).

I've already tried:

<html>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</html>

and

<?php
header
('Content-type: text/html; charset=utf-8');
?>

but nothing, any help would be really appreciated

@ch

I have tryied adding the

andrés chandía's picture

I have tried adding the tables to the drupal database but happens the same.

Any clue?

@ch

Central Ohio

Group organizers

Group categories

Topics

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: