2

I am using drupal in order to create a website on my localhost machine.

I have a mysql database that i want to connect it to the drupal website and display its data in a table.

and in the drupal I download the Views Database Connector (VDC) module in order to connect to the external database.

but it did not work .

second method I used views and created content types that are the fields table of the external databases.

but I do not know how to continue to display the data.

third method I created the tables in drupal database and want to display its content .

I am new to drupal so I want any of these method to work for me

note feeds module also did not work because its not set up yet on the drupal 8.x

6
  • Maybe you want to give this module a try also ... it has a DEV version for D8. If your question was about D7, you'd have a perfect solution (also as per your "I am new to Drupal" ...) Commented Jan 30, 2017 at 13:11
  • @Pierre.Vriens so if i use the D7 i will find more solutions to my questions?? can i downgrade drupal version without having issues? Commented Jan 30, 2017 at 13:25
  • Maturity for contributed D7 modules is about 5 years ahead of D8 equivalents, so pretty sure you'll find many more solutions. In that case the module I recommended (disclosure: I'm a co-maintainer) is a perfect fit for what you're looking for. But: to downgrade from D8 to D7 is an extremely interesting challenge (which I have never encountered before). So starting from scratch might be your only alternative (possibly combined with import/export of content created already in D8). Commented Jan 30, 2017 at 13:37
  • @Pierre.Vriens i start from scratch with drupal 7 so now what is the best way to import data from external database or create a table within the drupal database or import csv file using feeds ? although i will use these retrieved data in future to connected for example to google map or to an image. Commented Jan 30, 2017 at 14:26
  • Your question here is about D8 (so I cannot post a D7 answer to it ...). Maybe you want to look at my answer to this question Commented Jan 30, 2017 at 14:55

1 Answer 1

2

You need add the followings lines in settings.php for connecting 'external' database.

$databases['default']['default'] = array(
      'database' => 'drupal8',
      'username' => 'username',
      'password' => 'password',
      'prefix' => '',
      'host' => 'localhost',
      'port' => '3306',
      'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
      'driver' => 'mysql',
    );

$databases['external']['default'] = array(
  'database' => 'external',
  'username' => 'username',
  'password' => 'password',
  'prefix' => '',
  'host' => 'localhost',
  'port' => '3306',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql',
);

To activate external database

\Drupal\Core\Database\Database::setActiveConnection('external');

//code to fetch data and display

To activate drupal database

\Drupal\Core\Database\Database::setActiveConnection();

If you are looking some example refer How to connect to another database in your Drupal 8 site ?

4
  • what about if i have the tables inside the drupal database how to display its content and where the php file exist in order to write the required code ...not the settings.php Commented Jan 30, 2017 at 13:05
  • 1
    It depends what you're trying to achieve
    – Clive
    Commented Jan 30, 2017 at 13:10
  • @Clive i have an excel file that i need to display its content on the website like a table so what i did i converted to csv file in order to use feeds to import it but this method did not work. so i imported the file to the drupal database and create 4 content types as they are the fields of the table .... after that i did not know how to show the content of this database Commented Jan 30, 2017 at 13:16
  • @Jihad3aramouni, if you've imported the data to your drupal database you need to define the data structure of the table in hook_views_data api.drupal.org/api/drupal/core!modules!views!views.api.php/… and then it will be visible in views. - Not to be confused with content types. Here you need to import the csv data directly with migrate (drupal.org/project/migrate_source_csv) or feeds when this is ready for D8.
    – 4uk4
    Commented Jan 30, 2017 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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