1

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 $Course\n");
}



# 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?

11
  • 2
    Why don't you query your database with JavaScript..? Embedding php into a nodejs app just feels utterly wrong Commented Nov 17, 2017 at 10:53
  • What security reason? The node application runs on your server (I guess) so it's as secure as your php script. Commented Nov 17, 2017 at 10:56
  • @whiterabbitj it feels wrong because there is no need to manually start up a php process to do something that can easily be done in the original script Commented Nov 17, 2017 at 11:01
  • well now i know how to do it without ever asking it.. but indeed i would save this case for absolute necessity, like a very complicated PHP script already written. I guess exec is like eval, it can really go wrong if subtle parameters are altered Commented Nov 17, 2017 at 11:02
  • Why not doing it directly in php then? How is node helping? Honest question, not trying to be snarky. Commented Nov 17, 2017 at 11:05

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

server.js is that the whole thing with my code deleted? ?
Ok thanks :) will try it out asap. BTW data-php is that the filename or the directory of my php file?
data-php is the name of the route. The name of your PHP file is data.php. (you do well to ask, updated a typo in route)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.