PHP脚本返回数据

时间:2013-07-15 09:27:41

标签: php sql localhost

我已从网上下载此脚本以获取数据。我在我的java代码中使用这些数据。事实是我没有任何PHP经验,所以请不要低估这个问题。我无法弄清楚这个脚本中的问题

这就是这个php脚本返回并导致java中的JsonExceptions。

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 />

此脚本导致问题,因为它没有从db返回正确的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 {
?>

我非常感谢你的宝贵帮助。

4 个答案:

答案 0 :(得分:1)

你错过了你的php脚本中关闭的其他地方。

} 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
}

答案 1 :(得分:0)

问题是else没有结束

} else {
} //here
?>

答案 2 :(得分:0)

您遇到的解析错误意味着存在语法错误。我不知道,因为这不是所有的代码,但我认为你有一个缺失的大括号'{'

答案 3 :(得分:0)

因为我遵循相同的教程并且遇到了同样的问题,所以只需复制并粘贴它: 它没有任何错误。

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;
     }
 }