1

I am working on a project attendance system and i want to access a variable that is declared in php file require.php in my node.js file custom.js how should i achieve that? so far i have created a connection with my sql server and hard-coded the value of

`var company_name="Aaqoo 2";`

and everything is working fine but i dont want to hard-code it i want to fetch the company_name stored in my php variable $sup_company_name which is declared in my require.php file so my question is how should i fetch the value from $sup_company_name in my js variable.....kindly help me then i will ask futher questions

this is my node js code

var express = require('express');
var mysql = require('mysql');
var app = express();

var connection = mysql.createConnection({
    host:'localhost',
    user:'root',
    password:'',
    database:'attendance'
});

connection.connect(function(error) {
    if (!!error) {
    console.log('Error in connection');
    } else {
    console.log('Connected');  
    }
});
var company_name = "Aaqoo 2"; //here i want the value from php variable $sup_company_name;

connection.query("Select * from employee_leaves where employee_leave_company_name=?", [company_name], function(error,rows,fields) {
    if (!!error) {
        console.log("Error in the query");
    } else {
        console.log("succesfully done\n");
        console.log(rows);
    }
});

app.listen(1337);
13
  • Have you tried this? var company_name= '<?php echo $sup_company_name; ?>' Commented Jul 31, 2016 at 13:45
  • @Oluwafemi but the issue is $sup_company_name is in require.php file and when i include the require php file using include("require.php"); node.js gives me error Commented Jul 31, 2016 at 13:47
  • what is the error? Commented Jul 31, 2016 at 13:48
  • 1
    PHP is executed before JavaScript. Include your PHP file via PHP first, then echo the variable value Commented Jul 31, 2016 at 13:51
  • @Oluwafemi SyntaxError: Unexpected token <? ?> at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:373:25) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12) at Function.Module.runMain (module.js:441:10) at startup (node.js:139:18) at node.js:968:3 Commented Jul 31, 2016 at 13:51

1 Answer 1

1

I did it, This is my example, only that i use this for my conection with pgsql.

code node js

var exec = require('child_process').exec;

exec(
    'php -r \'include("require.php"); echo $companyname."#@#".$hostname."#@#".$username."#@#".$database."#@#".$password;\'',

  function (err, stdout, stderr) {
    var [ companyname,hostname,username, database, password ] = stdout.split('#@#');
    const connectionData = {
      user: username,
      host: '',
      database: database,
      password: password,
      port: 5432,
    }
    const client = new Client(connectionData)

    client.connect()
    client.query('Select * from employee_leaves where employee_leave_company_name=?',companyname)
        .then(response => {
            //console.log(response.rows)
            client.end()
        })
        .catch(err => {
            client.end()
        })
  }
);

require.php


$companyname= 'namecompany'; 
$hostname = 'localhost';
$database = 'namedatabase';
$username = 'root';
$password = '123';

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

Comments

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.