我正在研究项目考勤系统,我想访问我的node.js文件中的php文件require.php中声明的变量custom.js我该如何实现?到目前为止,我已经创建了与我的SQL服务器的连接并硬编码
的值`var company_name="Aaqoo 2";`
并且一切正常但我不想硬编码我想要获取存储在我的php变量$ sup_company_name中的company_name,这是在我的require.php文件中声明的所以我的问题是我应该如何从中获取值我的js变量中的$ sup_company_name .....请帮助我,然后我会问更进一步的问题
这是我的节点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);
答案 0 :(得分:1)
我做到了,这是我的例子,只是我将它用于与pgsql的连接。
代码节点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';