Answer the question
In order to leave comments, you need to log in
How to properly convert php file to node.js?
Hello,
I have a PHP file that I want to convert to Node.js.
How can I do it better?
About the file:
php-file collects data from the database, processes and outputs the result in json. the site pulls this file every second.
The idea is to completely abandon this file and move its contents to node.js under socket.io.
the problem is how to transfer these requests to the database from php to node.js?
The file is like this:
<?php
header("Content-Type: text/html; charset=UTF-8");
date_default_timezone_set('Europe/Helsinki');
include_once "pass.php";
$result = $dbh->query("SELECT * FROM `mod_1`");
$result->setFetchMode(PDO::FETCH_NUM);
$row = $result->fetch();
$unix = time();
$n = array();
$n[0] = $row[1]; // unix db
$n[1] = $row[2]; // t1
$n[2] = $row[3]; // t2
$n[3] = $row[4]; // t3
$n[4] = $row[5]; // m1
$n[5] = $row[6]; // m2
$n[6] = $row[7]; // pw
$n[7] = $row[8]; // vt
$n[8] = $row[9]; // c1
$n[9] = $row[10];// c1_unix
$n[10] = $row[11];// c2
$n[11] = $row[12];// c2_unix
$n[12] = $row[13]; // h1
$n[13] = $row[14];// h1_unix
$n[14] = $row[15];// h2
$n[15] = $row[16];// h2_unix
$result = $dbh->query("SELECT * FROM `power` ORDER BY `id` DESC LIMIT 1")->fetch();
$n[16] = $result[2];// power start
$today = mktime(0, 0, 0, date("m") , date("d"), date("Y"));
$d = date('d',$unix)-1;
$month = mktime(0, 0, 0, date("m"), date("d")-$d, date("Y"));
$n[17] = $dbh->query("SELECT sum(total) FROM `power` WHERE `start` > '".$today."'")->fetchColumn();
if($n[17] == NULL) $n[17] = 0;// power total
$n[18] = $dbh->query("SELECT sum(total) FROM `power` WHERE `start` > '".$month."'")->fetchColumn();
$n[19] = $dbh->query("SELECT sum(total) FROM `power`")->fetchColumn();
$n[20] = $dbh->query("SELECT sum(total) FROM `switch_1` WHERE `start` > '".$today."'")->fetchColumn();
if($n[20] == NULL) $n[20] = 0;
$n[21] = $dbh->query("SELECT sum(total) FROM `switch_1` WHERE `start` > '".$month."'")->fetchColumn();
$n[22] = $dbh->query("SELECT sum(total) FROM `switch_1`")->fetchColumn();
$n[23] = $dbh->query("SELECT sum(watt) FROM `switch_1` WHERE `start` > '".$today."'")->fetchColumn();
$n[24] = $dbh->query("SELECT sum(watt) FROM `switch_1` WHERE `start` > '".$month."'")->fetchColumn();
$n[25] = $dbh->query("SELECT sum(watt) FROM `switch_1`")->fetchColumn();
$n[27] = $dbh->query("SELECT sum(total) FROM `switch_2` WHERE `start` > '".$today."'")->fetchColumn();
if($n[27] == NULL) $n[27] = 0;
$n[28] = $dbh->query("SELECT sum(total) FROM `switch_2` WHERE `start` > '".$month."'")->fetchColumn();
$n[29] = $dbh->query("SELECT sum(total) FROM `switch_2`")->fetchColumn();
$n[30] = $dbh->query("SELECT sum(watt) FROM `switch_2` WHERE `start` > '".$today."'")->fetchColumn();
$n[31] = $dbh->query("SELECT sum(watt) FROM `switch_2` WHERE `start` > '".$month."'")->fetchColumn();
$n[32] = $dbh->query("SELECT sum(watt) FROM `switch_2`")->fetchColumn();
$n[33] = $n[23] + $n[30];
$n[34] = $n[24] + $n[31];
$n[35] = $n[25] + $n[32];
$dbh = null;
echo json_encode($n);
?>
Answer the question
In order to leave comments, you need to log in
how to transfer these requests to the database from php to node.js
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question