Skip to content
Advertisement

Running PHP in Node and parsing the data back to index.html

I am running PHP in node (Why not experimenting I know its crazy) to retrieve some data using ODBC connection.

This is the node server

Webserver.js

var express = require('express');
var app = express();
var R = require('ramda');

var execPHP = require('./execphp.js')();

execPHP.phpFolder='C:\users\public\data\';

app.use('*.php', function(request,response,next) {
    execPHP.parseFile(re

quest.originalUrl,function(phpResult) {
            var phpData = response.write(phpResult);
            response.end();
        });
    });
     
    app.listen(3000, function () {
        console.log('Node server listening on port 3000!');
    });


Executes PHP 

execphp.js

    /**
*
*/
class ExecPHP {
    /**
    *
    */
    constructor() {
        this.phpPath = 'C:/php/php.exe';
        this.phpFolder = '';
    }   
    /**
    *
    */
    parseFile(fileName,callback) {
        var realFileName = this.phpFolder + fileName;
        
        console.log('parsing file: ' + realFileName);

        var exec = require('child_process').exec;
        var cmd = this.phpPath + ' ' + realFileName;
        
        exec(cmd, function(error, stdout, stderr) {
            callback(stdout);
        });
    }
}
module.exports = function() {
    return new ExecPHP();
};

php data

 $connect = odbc_connect("sqltest", "", "");



$query = "SELECT [Mand]
,[Category]
,[Course]
FROM [Trt].[dbo].[ListViewMain]";



# perform the query
$result = odbc_exec($connect, $query);



# fetch the data from the database
while(odbc_fetch_row($result)){
  $MandStat = odbc_result($result, 1);
  $Category = odbc_result($result, 2);
  $Course= odbc_result($result, 3);
  print("$Mand  $Category $Coursen");
}



# close the connection
odbc_close($connect);
?>

My questiong would be how do i get the data from my localhost:3000/data.php to my index.html

What would be the best approach to solve this issue?

Advertisement

Answer

In this case, I think the best approach would be to make an AJAX call from your index.html to query the 'data-php' route and then display the data.

If I understand correctly, here’s a simpler example of what you are trying to do:

server.js

const express = require('express');
const app = express();

const execPHP = require('./execphp.js')();

app.get('/data-php', function (req, res) {
  execPHP.getODBCdata(function (phpResult) {
    res.json(phpResult);
  });
});

app.listen(3000, function() {
  console.log('Node server listening on port 3000!');
});

execphp.js

const { exec } = require('child_process');

class ExecPHP {

  constructor() {
    this.phpPath = 'C:/php/php.exe';
  }   

  getODBCdata (callback) {
    let cmd = this.phpPath + ' data.php';
    exec(cmd, function (error, stdout, stderr) {
      callback(stdout);
    });
  }
}

module.exports = function() {
  return new ExecPHP();
};

index.html

$.get("/data-php", function (data) {
  $("#result").html(data);  // here you display the data
});

Otherwise, you could also consider to use a template engine like handlebars to render your html.

And keep in mind that it is much more a workaround, as stated in the comments, than a clean and robust solution. It would be better to find a ODBC driver that works on Nodejs.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement