所以我正在为我的游戏编写一个帐户登录区域,并在我的网站上使用json get action调用log.php。 json操作看起来像http://domain.com/log.php?fname='"&myUserName&"'&fpass='"&myPassword&"'
log.php内容如下。
<?php header('Access-Control-Allow-Origin: *');
$username = $_GET['fname'];
$password = $_GET['fpass'];
$connect = mysqli_connect("localhost","user","pass","db") or die("Could not connect");
$sql = "SELECT id FROM members WHERE username = '$username' and password = '$password'";
$result = mysqli_query($connect, $sql);
if($result === FALSE) {
die(mysql_error());
}
while ($row = mysqli_fetch_array($result)) {
echo $row['id'];
}
mysql_close($connect);
?>
我没有得到任何错误,但它没有返回id,假设在游戏中设置变量“userID”。它仍然是空白的。任何想法如何让这个工作?
由于
答案 0 :(得分:1)
更改为
$username = $_GET['fname'];
&#13;
答案 1 :(得分:1)
您需要使用json_encode()函数生成JSON响应。
$username = $_GET['fname'];
$password = $_GET['fpass'];
$connect = mysqli_connect("localhost","user","pass","db") or die("Could not connect");
$sql = "SELECT id FROM members WHERE username = '$username' and password = '$password'";
$result = mysqli_query($connect, $sql);
if($result === FALSE) {
die(mysql_error());
}
while ($row = mysqli_fetch_array($result)) {
$res = array("userID"=> $row['id']);
}
mysql_close($connect);
echo $res_json = json_encode($res);
?>
在您的客户端,您将使用解析JSON&amp; amp; data.userID
答案 2 :(得分:-1)
没关系,我在编码伙伴的帮助下解决了这个问题。如果你们有兴趣寻找,可以解决这个问题。
<?php
header('Access-Control-Allow-Origin: *');
error_reporting(0);
require 'connection.php';
$username = $_GET["fname"];
$password = $_GET["fpass"];
$safeuser = $db->real_escape_string(md5($username));
$safepass = $db->real_escape_string(md5($password));
if($result = $db->query("SELECT * FROM `members` WHERE username='".$safeuser."' AND password='".$safepass."'")){
if ($count = $result->num_rows){
while($row = $result->fetch_assoc()){
echo $row['id'];
}
}
}
$db->close();
?>