简体   繁体   English

如何从node.js中的php变量中获取值

[英]How to get value from php variable in node.js

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?我正在开发一个项目考勤系统,我想访问在 node.js 文件 custom.js 中的 php 文件 require.php 中声明的变量,我应该如何实现? so far i have created a connection with my sql server and hard-coded the value of到目前为止,我已经创建了与我的 sql server 的连接并硬编码了

`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一切正常,但我不想对其进行硬编码我的js变量.....请帮助我然后我会问更多的问题

this is my node js code这是我的节点 js 代码

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);

I did it, This is my example, only that i use this for my conection with pgsql.我做到了,这是我的示例,只是我将它用于与 pgsql 的连接。

code node js代码节点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要求.php


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM