简体   繁体   English

Android-检查MySQL中的SELECT语句返回值

[英]Android - Check if SELECT Statement Return Value in MySQL

I will explain what I want to do before getting in the codes, I have Android project, connected to MySQL database, and I have a password recovery activity in my app, in this activity the user will type his email in order to get his log in information, so when the user click send the app will check if the email exists in the database or not, if yes will send an email and if no will notify the user. 在获取代码之前,我将解释我要做什么,我有一个Android项目,已连接到MySQL数据库,并且我的应用程序中有一个密码恢复活动,在此活动中,用户将键入他的电子邮件以获取他的日志。信息,因此当用户单击发送时,应用程序将检查数据库中是否存在电子邮件,如果是,则发送电子邮件,如果否,则通知用户。

Now, in the PHP side every thing works will, I wrote the query and it returns correct values. 现在,在PHP方面,一切正常,我编写了查询,并返回了正确的值。

public function getUName($UEmail) {
    if ($UEmail != NULL) {
        $data = array();    
        $sql = "Select UName FROM `user` WHERE `Email`= '" . $UEmail . "'"; 
        $result = $this->db->query($sql);
                    while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
        $data[] = $row;
    }

    return $this->_jsonEncode( $data );

    }
}

I know that in PHP if I want to check if the SELECT statement return value or not I will write something like: 我知道在PHP中,如果我想检查SELECT语句是否返回值,我将编写如下内容:

if (mysql_num_rows($result) > 0) 

But what about in Android side? 但是在Android方面呢? I have this function: 我有这个功能:

private boolean getUserInfo(String uEmail)
{
    String getInfo_URL = SERVER_URL + "GetInfo.php?email=" + uEmail;
    new CommunicatetoServer().execute(getEmail_URL);
    return true;
}

I want this function to return true if the SELECT statement returned value, and false otherwise. 我希望此函数在SELECT语句返回值的情况下返回true,在其他情况下返回false。

Thank you in advance. 先感谢您。

new CommunicatetoServer().execute(getEmail_URL);

You must implement CommunicatetoServer in such way, that after executing http request, it will get data received from the server. 您必须以这种方式实现CommunicatetoServer,即在执行http请求之后,它将获取从服务器接收的数据。

You really need implement HTTP request in another thread, for example using AsyncTask (or using external libs for http requests) and to use Callback that will be triggered when CommunicatetoServer will receive data returned from the server. 您确实确实需要在另一个线程中实现HTTP请求,例如使用AsyncTask(或对HTTP请求使用外部库),并使用在CommunicatetoServer接收到服务器返回的数据时将触发的回调。

In your case, PHP script returns JSON data. 就您而言,PHP脚本返回JSON数据。 You just need to parse received JSON data to an java object representation. 您只需要将接收到的JSON数据解析为Java对象表示即可。

You can use for example GSON library for JSON parsing in Android 您可以使用例如GSON库在Android中进行JSON解析

Some notes: 一些注意事项:

  • Your PHP code is very bad. 您的PHP代码非常糟糕。 It's insecure - you have SQL injection vulnerability 这是不安全的-您具有SQL注入漏洞
  • Is field "Email" unique in DB? DB中的“电子邮件”字段是否唯一? (It must be) - If yes, use fetch_row() instead fetch_array() (必须是)-如果是,请使用fetch_row()代替fetch_array()

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

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