简体   繁体   English

查询从postman和phpmyadmin运行,但不是从android运行

[英]Query run from postman and phpmyadmin but not from android

Situation 情况

I am using mysql database. 我正在使用mysql数据库。 Query runs from phpmyadmin and also from postman 查询从phpmyadmin和邮递员运行

But when i send a request from android(It returns ZERO row) 但是当我从android发送请求时(它返回ZERO行)

I have logged email sent from android is correct and works with other queries but not this one 我已经记录了从android发送的电子邮件是正确的,并与其他查询一起工作但不是这个

    public function isUserExists($email, $u_name) {
    $stmt = $this->conn->prepare("select * from login where email_id = ?");                          
     $stmt->bind_param("s",$email);
    $stmt->execute();
    $stmt->store_result();
    $num_rows = $stmt->num_rows; //getting no of rows if exits
    $stmt->close();
    return $num_rows > 0;
}

Question

Why this is not working even correct email is send from android and succsessfully get it in php 为什么这不工作甚至正确的电子邮件是从Android发送和succsessfully得到它在PHP

The email we send from android is work perfectly in all other queries and methods 我们从android发送的电子邮件在所有其他查询和方法中都能很好地工作

Edit This class i am using to send my post request 编辑此类我用来发送我的帖子请求

public class WebConnector {
    String boundary = "-------------" + System.currentTimeMillis();
    private static final String LINE_FEED = "\r\n";
    private static final String TWO_HYPHENS = "--";




    private StringBuilder url;
    private String api_key;
    private HashMap<String, String> params = new HashMap<>();
    File file;

    private int count = 0;
    private DataOutputStream dos;
    JSONObject postData;

    public  void addParams(String key , String value) {
     params.put(key,value);
    }
    public WebConnector(StringBuilder url, String api_key)
    {
        this.url = url;
        this.api_key = api_key;
        this.postData = new JSONObject();
        this.file = null;
    }
    public WebConnector(StringBuilder url, String api_key, JSONObject postData)
    {
        this.url = url;
        this.api_key = api_key;
        this.postData = postData;
        this.file = null;
    }


    public WebConnector(StringBuilder url, String api_key, JSONObject postData, File image) {
        super();
        this.url = url;
        this.postData = postData;
        this.api_key = api_key;
        this.file = image;

    }


    public String connectToMULTIPART_POST_service(String requestMethod) {
        createServiceUrl();

        System.out.println(">>>>>>>>>url : " + url);



        String strResponse = "";
        InputStream inputStream = null;
        HttpURLConnection urlConnection = null;

        try {
            urlConnection = (HttpURLConnection) new URL(url.toString()).openConnection();
            urlConnection.setRequestProperty("Content-Type", "application/json");
            urlConnection.setRequestProperty("Connection", "close");
            urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 ( compatible ) ");
            urlConnection.setRequestProperty("Authorization", "" + api_key);

            urlConnection.setRequestMethod(requestMethod);

            if(requestMethod.equals("GET") || requestMethod.equals("DELETE"))
            urlConnection.setDoOutput(false);
            else {
                urlConnection.setRequestProperty("Content-type", "multipart/form-data; boundary=" + boundary);
                urlConnection.setDoOutput(true);

                urlConnection.setDoInput(true);
                urlConnection.setUseCaches(false);
                urlConnection.setChunkedStreamingMode(1024);

                dos = new DataOutputStream(urlConnection.getOutputStream());
                Iterator<String> keys = postData.keys();
                while (keys.hasNext()) {
                    try {
                        String id = String.valueOf(keys.next());
                        addFormField(id, postData.get(id).toString());
                        System.out.println(id + " : " + postData.get(id));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                try {

                    dos.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                if (file != null)
                    addFilePart("url", file);

                build();
            }
            urlConnection.connect();
            int statusCode = 0;
            try {
                urlConnection.connect();
                statusCode = urlConnection.getResponseCode();
            } catch (EOFException e1) {
                if (count < 5) {
                    urlConnection.disconnect();
                    count++;
                    String temp = connectToMULTIPART_POST_service(requestMethod);
                    if (temp != null && !temp.equals("")) {
                        return temp;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            // 200 represents HTTP OK
            if (statusCode >=400) {
                inputStream = new BufferedInputStream(urlConnection.getErrorStream());
                strResponse = readStream(inputStream);
            } else {
                System.out.println(urlConnection.getResponseMessage());
                inputStream = new BufferedInputStream(urlConnection.getInputStream());
                strResponse = readStream(inputStream);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != inputStream)
                    inputStream.close();
            } catch (IOException e) {
            }
        }

        return strResponse;
    }


    public void addFormField(String fieldName, String value) {
        try {
            dos.writeBytes(TWO_HYPHENS + boundary + LINE_FEED);
            dos.writeBytes("Content-Disposition: form-data; name=\"" + fieldName + "\"" + LINE_FEED + LINE_FEED/*+ value + LINE_FEED*/);
            /*dos.writeBytes("Content-Type: text/plain; charset=UTF-8" + LINE_FEED);*/
            dos.writeBytes(value + LINE_FEED);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void addFilePart(String fieldName, File uploadFile) {
        try {
            dos.writeBytes(TWO_HYPHENS + boundary + LINE_FEED);
            dos.writeBytes("Content-Disposition: form-data; name=\"" + fieldName + "\";filename=\"" + uploadFile.getName() + "\"" + LINE_FEED);
            dos.writeBytes(LINE_FEED);

            FileInputStream fStream = new FileInputStream(uploadFile);
            int bufferSize = 1024;
            byte[] buffer = new byte[bufferSize];
            int length = -1;

            while ((length = fStream.read(buffer)) != -1) {
                dos.write(buffer, 0, length);
            }
            dos.writeBytes(LINE_FEED);
            dos.writeBytes(TWO_HYPHENS + boundary + TWO_HYPHENS + LINE_FEED);
        /* close streams */
            fStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void addHeaderField(String name, String value) {
        try {
            dos.writeBytes(name + ": " + value + LINE_FEED);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void build() {
        try {
            dos.writeBytes(LINE_FEED);
            dos.flush();
            dos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String readStream(InputStream in) {
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String nextLine = "";
            while ((nextLine = reader.readLine()) != null) {
                sb.append(nextLine);
            }
        /* Close Stream */
            if (null != in) {
                in.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return sb.toString();
    }

    private void createServiceUrl() {
        if (null == params) {
            return;
        }
        final Iterator<Map.Entry<String, String>> it = params.entrySet().iterator();
        boolean isParam = false;
        while (it.hasNext()) {
            final Map.Entry<String, String> mapEnt = (Map.Entry<String, String>) it.next();
            url.append(mapEnt.getKey());
            url.append("=");
            try {
                url.append(URLEncoder.encode(mapEnt.getValue(), "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
            url.append("&");//%20
            isParam = true;
        }
        if (isParam) {
            url.deleteCharAt(url.length() - 1);
        }
    }
    //localhost/LumnuOirtal/event?event=1&descip=wdsdsdsd&

}

I believe the issue is in the "bindParam" statement. 我相信问题出在“bindParam”声明中。 Your statement says to replace "s" instead of "?". 你的陈述说要替换“s”而不是“?”。 Try this instead: 试试这个:

$stmt = $this->conn->prepare("select * from login where email_id = ?");
$stmt->bind_param("?",$email);

OR 要么

$stmt = $this->conn->prepare("select * from login where email_id = :email");
$stmt->bind_param(":email",$email);

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

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