简体   繁体   English

在Node.js中如何从Php检索数据

[英]How to retrieve Data from Php while in Node.js

I just wanted to retrieve some PHP/Mysql stuff at the beginning of my app (authentication and x and y data) from the users which I later plan to emit to the app.js (one time at the beginning and once the user disconnects update x - y values). 我只是想从用户那里检索我的应用程序开头的一些PHP / Mysql东西(身份验证以及x和y数据),后来我打算将它们发送给app.js (一次是一次,一旦用户断开更新, x - y值)。

So basically I have set up Nodes.js and understood that some stuff is not possible like before (eg with plain php) 所以基本上我已经设置了Nodes.js并了解有些东西不可能像以前一样(例如使用纯PHP)

Where I already have a problem is the AJAX php request in the index.html of my nodes Server 我已经遇到问题的地方是节点服务器的index.html中的AJAX php请求

Schema: 架构:

app.js : pull Data from the /Client/index.html (I think need to do it via sockets) app.js :从/Client/index.html提取数据(我认为需要通过套接字进行操作)

index.html : get or post data via Ajax to a php file and get the values of the database back to the index.html (JavaScript) then send that data via sockets to the app.js index.html :通过Ajax获取或发布数据到php文件,并将数据库的值返回给index.html (JavaScript),然后通过套接字将该数据发送到app.js

php: select mysql database retrieve values from mysql parse them via Json and make them available in the index.html file Nodes.js (Client) php:选择mysql数据库从mysql检索值,并通过Json解析它们,并使它们在index.html文件Nodes.js中可用(客户端)

Maybe somebody of you have a solution 也许你们有一个解决方案

Nodes.js /Client/index.html : Nodes.js /Client/index.html

function checkuser(username, password) {
    var myObj;
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            // Typical action to be performed when the document is ready:
            myObj = xhttp.responseText;
            var i = 0;
            while (i <= myObj.length) {
                //console.log("ASQL found and Auth Username:"+ myObj[i].username)  ;
                console.log(myObj.username);
                i++;
            }
        }
    };
    xhttp.open("GET", "/client/is_user.php?username333=" + username + "&password333=" + password, true);
    xhttp.send();
}

is_user.php : is_user.php

<?php
require('config_sql.php');
$email = stripslashes($_GET['username333']);
$email = mysqli_real_escape_string($con,$email);
$password369 = stripslashes($_GET['password333']);
$password369 = mysqli_real_escape_string($con,$password369);
$query = "SELECT * FROM `users` WHERE email='$email'
and password='".md5($password369)."'";
$result = mysqli_query($con,$query) or die(mysql_error());
$response = array();
$rows = mysqli_num_rows($result);
while ($row_user= mysqli_fetch_assoc($result))
{
    $response[] = $row;
}
$jsonData = json_encode($response); 
echo $jsonData;
mysqli_close($con);
?>  

atm is not retrieving the username form the created json on the php side. atm不在php端从创建的json检索用户名。 If I console.log(myObj); 如果我console.log(myObj); it's showing the me complete php.file data as plain text if I want to retrieve the username from MySql its saying undefined . 如果我想从MySql检索用户名undefined则以纯文本形式显示完整的php.file数据。

Is the php Interpreter actually working when I post/get via Ajax in a Node.js environment? 当我在Node.js环境中通过Ajax post/get时,php解释器实际上是否正常工作? Normally when I was programming with pure php all the request worked well. 通常,当我使用纯PHP进行编程时,所有请求都运行良好。

Thank you in advance. 先感谢您。

Check you code for recieving result from query: 检查您的代码以获取查询结果:

$rows = mysqli_num_rows($result);
while ($row_user= mysqli_fetch_assoc($result))
{
$response[] = $row;
}

$jsonData = json_encode($response); $ jsonData = json_encode($ response);

Should be so: 应该是这样的:

$rows = mysqli_num_rows($result);
while ($row_user= mysqli_fetch_assoc($result))
{
$response[] = $row_user;
}

$jsonData = json_encode($response); $ jsonData = json_encode($ response);

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

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