I have a simple MYSQL table.

SELECT * FROM countries;

+----------------------------------------------------+
| id | country                                |
+----------------------------------------------------+
| 1| United Kingdom                       |
| 2| United States                         |
| 3| Afghanistan                            |
| 4| Albania                                  |
+----------------------------------------------------+

I've searched hight & low.

What is the best way to query a MYSQL table (countries) and display the results in a Drupal page?

Comments

sprite’s picture

spritefully yours
Technical assistance provided to the Drupal community on my own time ...
Thank yous appreciated ...

dibyajyoti.mallick’s picture

$countryQuery = db_select('countries', 'cnt');
$countryData = $countryQuery
->fields('cnt', array('id','country'))
->orderBy('cnt.id', 'DESC')
->execute()->fetchAll();

Put it in your custom module and print $countryData, you can fetch all record from DB

mdi45’s picture

I appreciate your help, but I'm hoping that someone could write a very simple tutorial, similar to the list below:

1. Create the following mysql database, with the following contents:

name: hello_world_db
username: hello
password: world

SELECT * FROM hello_world_table;

+----------------------------------------------------+
| id | hello_text |
+----------------------------------------------------+
| 1| hello |
| 2| world |
+----------------------------------------------------+

2. Create files:
custom_module.module
custom_module.info

with contents

3. Install module

4. Create a Basic Article & show how to paste PHP code (if that is preferred way) or through hook & schema (if that is preferred way), so that you can view the hello_world_database contents.

I'm hoping that someone can write a very simplified tutorial similar to this. Thank you.

sprite’s picture

People here are not going to write your code for you.

The free community help generously provided here will point you to tutorials about Drupal module building and so forth, but we are not going to build your module for you.

Read the Drupal 8 development docs at the links and watch some Drupal 8 module development tutorials on youtube.

spritefully yours
Technical assistance provided to the Drupal community on my own time ...
Thank yous appreciated ...

sachinsuryavanshi’s picture

Hello,

This is very good post for Database activities with Drupal 8 concern.http://www.eilyin.name/note/database-queries-drupal-8-7

And yes, we can only help. Writing full base code wont be possible here.

Thanks,
Sachin