Am coming from php background, now learning on how to move php mysql queries to nodejs. i have learnt how to work with nodejs insert, update and delete.
Now i have the code in php which returns queries in json format. what I want is how can I re-write this to nodejs json format.
below is the working php
if($request == 1){
$result_arr = array();
$query = "SELECT * FROM user where email='[email protected]' ";
$result = mysqli_query($con,$query);
while($row = mysqli_fetch_array($result)){
$uid = $row['uid'];
$name = $row['username'];
// Checking user status
$status_query = "SELECT count(*) as cntStatus FROM user_count_table WHERE uid='$uid'";
$status_result = mysqli_query($con,$status_query);
$status_row = mysqli_fetch_array($status_result);
$count_status = $status_row['cntStatus'];
$result_arr[] = array("id" => $uid, "username" => $name, "my_count" => $total_count);
}
echo json_encode($result_arr);
exit;
}
below is my effort in trying to move the above php code to nodejs but cannot get it to work.
var connection = require('./config');
module.exports.getdata=function(req,res){
var email='[email protected]';
connection.query('SELECT * FROM users WHERE email = ?',[email], function (error, results, fields) {
uid==results[0].uid;
name==results[0].username;
connection.query('SELECT count(*) as cntStatus FROM user_count_table WHERE uid= ?',[uid], function (error, results, fields) {
total_count==results[0].cntStatus;
});
if (error) {
console.log('error');
res.json({
status:false,
message:'there are some error with the query'
})
}else{
result_arr[] = array("id" => uid, "username" => name, "my_count" => total_count);
console.log('query okay');
res.json({
//data:results,
data:result_arr
})
}
});
}