簡體   English   中英

在Node.js中如何從Php檢索數據

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

我只是想從用戶那里檢索我的應用程序開頭的一些PHP / Mysql東西(身份驗證以及x和y數據),后來我打算將它們發送給app.js (一次是一次,一旦用戶斷開更新, x - y值)。

所以基本上我已經設置了Nodes.js並了解有些東西不可能像以前一樣(例如使用純PHP)

我已經遇到問題的地方是節點服務器的index.html中的AJAX php請求

架構:

app.js :從/Client/index.html提取數據(我認為需要通過套接字進行操作)

index.html :通過Ajax獲取或發布數據到php文件,並將數據庫的值返回給index.html (JavaScript),然后通過套接字將該數據發送到app.js

php:選擇mysql數據庫從mysql檢索值,並通過Json解析它們,並使它們在index.html文件Nodes.js中可用(客戶端)

也許你們有一個解決方案

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

<?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不在php端從創建的json檢索用戶名。 如果我console.log(myObj); 如果我想從MySql檢索用戶名undefined則以純文本形式顯示完整的php.file數據。

當我在Node.js環境中通過Ajax post/get時,php解釋器實際上是否正常工作? 通常,當我使用純PHP進行編程時,所有請求都運行良好。

先感謝您。

檢查您的代碼以獲取查詢結果:

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

$ jsonData = json_encode($ response);

應該是這樣的:

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

$ jsonData = json_encode($ response);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM