简体   繁体   English

在数据库中找到数据并插入数据库后,PHP将成功返回给Java

[英]Php returning success to java after data found in database and on insert of database

<?php

include("db_config.php");

$myusername = $_GET['username'];
$mypassword = $_GET['password'];



$sql="SELECT id FROM tablename WHERE username='$myusername' and password='$mypassword'";

echo $sql;

$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$active=$row['active'];

$count=mysql_num_rows($result);


 // If result matched $myusername and $mypassword, table row must be 1 row
if($count==1)
{
 echo "<strong> <font size='18'>Login Success</font></strong>";
 }
 else 
 {
echo "<strong> <font size='18'>Login Failed</font></strong>";
}

?>

I tried response array on success.. But in java file wen I Receive it .. is showing Null.. I am using this line to receive the success 我尝试了成功的响应数组。。但是在wen文件中,我收到了..显示为Null。我正在使用此行来接收成功

  success=json.getInt(TAG_SUCCESS);

Also I used this in my JSON parser class to check if server returns success. 另外,我在JSON解析器类中使用它来检查服务器是否返回成功。

  if (response.getStatusLine().getStatusCode() == 200) 

And also The login is giving Login failed.. though I enter the username present in the database 而且登录名给登录失败..尽管我输入了数据库中存在的用户名

Can any1 help me?? 可以帮我吗?

Not sure what library you are using BUT using a method "getInt()" on an HTML text "Login Success/Failed" looks very wrong. 不确定在HTML文本“登录成功/失败”上使用方法“ getInt()”使用的是哪个库,但看起来很错误。

Using a "json." 使用“ json”。 method on HTML data also looks very wrong. HTML数据上的方法看起来也很错误。

So get your php to emit valid JSON, and, get your Java to extract the Login Success or Failed text from hte JSON. 因此,让您的php发出有效的JSON,并让您的Java从hte JSON中提取“登录成功”或“失败”文本。

Correct me if I am wrong, but your php doesn't return a valid JSON, instead it returns HTML code. 如果我错了,请纠正我,但是您的php不会返回有效的JSON,而是返回HTML代码。 Naturally when you try to parse it as JSON, you get null . 当您尝试将其解析为JSON时,自然会得到null If your PHP is changed to this: 如果您的PHP更改为此:

if($count==1) {
    $result['login'] == 'success';
}
else {
    $result['login'] == 'failed';
}
echo json_encode($result);

then you may be able to parse this in your java and use something like: 那么您也许可以在Java中解析此内容并使用类似以下内容的代码:

if("success".equals(json.getString("login")) {
    //login successful
}

This is not the only way to achieve this, but it should do the trick. 这不是实现此目的的唯一方法,但应该可以解决问题。

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

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