Using existing PHP code in a Drupal custom module

My agency is heading towards dropping our current web development and having expensive consultants create a new site in Drupal. I keep trying to understand Drupal and see its appeal, but it gets complicated quickly.

I’ve looked at Drupal charting modules, and they all seem to take input from CVS files. The big blind spot in Drupal developers seems to be getting data from SQL queries into their modules. There is something I found, the /data/content/entities Maybe that’s it. But I’m starting to test weather I can get my existing PHP routines in Drupal custom modules.

I used Aquia Dev Desktop to whip up a few Drupal 8 test sites on my local Windows 10 PC.

I was able to follow the instructions at… to get the Hello World custom module working on my install of Drupal 8.3.4 on a local Windows PC. Next I tried to stick a snippet of my php code into a custom module controller to see if I could print out 10 lines of a MySQL query.

Following the Hello World example, I set up a parallel folder structure to query_Prosser in modules\custom.

Then I created an yml file,

[code]name: Query Prosser Module
description: Creates a page testing a query of MySQL table tblProssercuryr, printing results from first 10 rows.
package: Custom

type: module
core: 8.x[/code]

At this point, the Query Prosser Module appeared in the Custom section of the Modules page.

Then I created a query_Prosser.routing.yml file:

query_Prosser.content: path: '/query' defaults: _controller: '\Drupal\query_Prosser\Controller \queryController::content' _title: 'Query Prosser' requirements: _permission: 'access content'

Then I tested a piece of php script on my remote web host that prints out the first 10 lines of a query of a MySQL table, tblProssercuryr. I copied the script lines into a public function of a php script, queryController, on my test Drupal site on local host. not knowing exactly why, I tried to imitate the Hello World example by adding the namespace, use, and class lines, and wraping my php code in a public function.


namespace Drupal\query_Prosser\Contoller;

use Drupal\Core\Controller\ControllerBase;

class queryController extends ControllerBase {

public function content() {


// P. Huffman 8-1-2017
// Retrieve and print a few MySQL records in preparation to trying this as a Drupal module.

//$year = $_GET[“year”];
//$yearprior = $year - 1;
//first read in adult chinook data from a mysql table at, database db28989a
$SQLstatement = “Select (Date), Chinook From tblProssercuryr
WHERE date > ‘$2017-04-01’ and date < '2017-11-15’
and Chinook IS NOT NULL
order by Date
limit 10”;
#Read in the data into arrays
@ $db = mysql_connect( “”, “username”, “password”, ‘db28989a’);
if (mysqli_connect_error())
echo ‘Error: Could not connect to database.’;
$result = mysql_db_query(“db28989a”, $SQLstatement);

$num_results = $result->num_rows;

#Initialize counters and arrays
$count = 0;
$chinooktotal = 0;
$date = array();
$chinook = array();
while ($row = mysql_fetch_row($result)) {
    print (sprintf ("date = %s, chinook = %d\n <br />",$row[0],$row[1]));
    $date[$count] = $row[0];
    $chinook[$count] = $row[1];
    $chinooktotal = $chinooktotal + $chinook[$count];



Then I thought I activated the custom module by clearing the cache, checking its box at http://drupal-8-3-4.dd:8083/admin/modules, and hitting the install button. But unlike the hello_world custom module that appeared at http://drupal-8-3-4.dd:8083/hello, http://drupal-8-3-4.dd:8083/query gave me a Drupal site template page with “Page not found” . What am I not understanding? Am I on the right track? I also tried not wrapping my php is a function.

Did you see anything interesting in the web server’s logs? These Drupal-native debugging tools may come in handy to debug your module:

Nothing useful in the logs, just Page not found. I’m already running Dev module because of the youtube tutorial I ran through.

I see that the Hello World example continues with more examples and exercises. I see what I can learn from that.

Oh, wait, when I went back to Drupal Extend to recheck the box on my custom module, and cleared the cache again, now I get Access denied in my drupal-8-3-4.dd:8083/query. I thought that was taken care of by the _permission: ‘access content’ in my query_prosser.routing.yml file.