返回数据的PHP脚本
为了获取数据,我从网上下载了这个脚本。我在java代码中使用这些数据。事实上,我在php方面没有任何经验,所以请不要否决这个问题。我无法找出这个脚本中的问题 这就是php脚本返回的内容,并导致java中出现JSONException返回数据的PHP脚本,php,sql,localhost,Php,Sql,Localhost,为了获取数据,我从网上下载了这个脚本。我在java代码中使用这些数据。事实上,我在php方面没有任何经验,所以请不要否决这个问题。我无法找出这个脚本中的问题 这就是php脚本返回的内容,并导致java中出现JSONException 07-15 14:18:46.725: I/System.out(1799): displaying <br /><b>Parse error</b>: syntax error, unexpected end of file i
07-15 14:18:46.725: I/System.out(1799): displaying <br /><b>Parse error</b>:
syntax error, unexpected end of file in <b>C:\xampp\htdocs\xmytestdb\login.php</b>
on line <b>64</b><br />
07-15 14:18:46.725:I/System.out(1799):显示分析错误:
语法错误,C:\xampp\htdocs\xmytestdb\login.php中的文件意外结束
第64行
此脚本导致问题,因为它没有从数据库返回正确的json格式数据
<?php
require("config.inc.php");
if (!empty($_POST)) {
$query = "
SELECT
id,
username,
password
FROM usernamepass
WHERE
username = :username
";
$query_params = array(
':username' => $_POST['username']
);
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error1. Please Try Again!";
die(json_encode($response));
}
$validated_info = false;
$row = $stmt->fetch();
if ($row) {
if ($_POST['password'] === $row['password']) {
$login_ok = true;
}
}
if ($login_ok) {
$response["success"] = 1;
$response["message"] = "Login successful!";
die(json_encode($response));
} else {
$response["success"] = 0;
$response["message"] = "Invalid Credentials!";
die(json_encode($response));
}
} else {
?>
我将非常感谢您的宝贵帮助。问题是有
否则
不会结束
} else {
} //here
?>
您错过了php脚本中的else的结尾,这里有这个
} else { //Here the else part is not closed.So you are getting the syntax error
必须这样改变
else { }
if-else
必须为
if(condition){
//Code here
}
else{
//Code here
}
您遇到的分析错误意味着存在语法错误。我不知道,因为这不是全部代码,但我认为您缺少了一个大括号“{”,因为我遵循了相同的教程,并且遇到了相同的问题,只需复制并粘贴以下内容: 它工作正常,没有任何错误
require("config.inc.php");
if (!empty($_POST)) {
$query = "SELECT id, username, password FROM users WHERE username = :username";
$query_params = array(
':username' => $_POST['username']);
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error1. Please Try Again!";
die(json_encode($response));
}
$validated_info = false;
$login_ok = false;
$row = $stmt->fetch();
if ($row) {
if ($_POST['password'] === $row['password']) {
$login_ok = true;
}
}
我试过了,但它没有返回任何东西。它返回空字符串。我可以把这个脚本放在哪里,这样当我转到脚本地址时,它会在浏览器中返回json格式的数据