Monday 15 June 2015

java - String passed from Android POST request to PHP in sevrer not being recognized properly in MySQL SELECT query -



java - String passed from Android POST request to PHP in sevrer not being recognized properly in MySQL SELECT query -

i've been hacking @ 1 while, can't find reason next behavior:

i have android app send multipart/form-data http post. request has next form:

private final string delimiter = "--"; private final string boundary = "swa" + long.tostring(system.currenttimemillis()) + "swa"; private final string charset = "utf-8"; private final string linespace = "\r\n"; private final string domain = (domain); private httpurlconnection configureconnectionformultipart(string url) throws malformedurlexception, ioexception { httpurlconnection con = (httpurlconnection) (new url(url)) .openconnection(); con.setrequestmethod("post"); con.setdoinput(true); con.setdooutput(true); con.setrequestproperty("connection", "keep-alive"); con.setrequestproperty("content-type", "multipart/form-data;boundary=" + boundary); homecoming con; } private void addformpart(string paramname, string value, dataoutputstream os) throws ioexception { os.writebytes(linespace + delimiter + boundary + linespace); os.writebytes("content-disposition: form-data; name=\"" + paramname + "\"" + linespace); os.writebytes("content-type: text/plain; charset=" + charset + linespace); os.writebytes(linespace + value + linespace); os.flush(); } private void addfilepart(string paramname, file data, dataoutputstream os) throws ioexception { os.writebytes(linespace + delimiter + boundary + linespace); os.writebytes("content-disposition: form-data; name=\"" + paramname + "\"" + linespace); os.writebytes("content-type: application/octet-stream\r\n"); os.writebytes("content-transfer-encoding: binary" + linespace); os.writebytes(linespace); os.flush(); fileinputstream fis = new fileinputstream(data); byte[] buffer = new byte[4096]; int bytesread = -1; while ((bytesread = fis.read(buffer)) != -1) { os.write(buffer, 0, bytesread); } os.writebytes(linespace); os.flush(); fis.close(); } private void finishmultipart(dataoutputstream os) throws ioexception { os.writebytes(linespace); os.flush(); os.writebytes(delimiter + boundary + delimiter + linespace); os.close(); } private string getresponse(httpurlconnection con) throws ioexception { string response = ""; int status = con.getresponsecode(); if (status == httpurlconnection.http_ok) { bufferedreader reader = new bufferedreader(new inputstreamreader( con.getinputstream())); string line = ""; while ((line = reader.readline()) != null) { response += line; } reader.close(); } else { bufferedreader reader = new bufferedreader(new inputstreamreader(con.geterrorstream())); string line = ""; while ((line = reader.readline()) != null) { response += line; } reader.close(); throw new ioexception("server returned non-ok status: " + status+", "+response); } homecoming response; } public searchqueryrunnable initsearchqueryrunnable(string query) { homecoming new searchqueryrunnable(query); } private class searchqueryrunnable implements runnable { private final string _query; private final string _url = domain + "search_query.php"; public searchqueryrunnable(string query) { _query = query; } @override public void run() { seek { httpurlconnection con = configureconnectionformultipart(_url); dataoutputstream os = new dataoutputstream( con.getoutputstream()); addformpart("tag", _query, os); finishmultipart(os); string result = getresponse(con); log.i("searchquery", result); con.disconnect(); } grab (malformedurlexception e) { e.printstacktrace(); } grab (ioexception e) { e.printstacktrace(); } } }

in search_query.php, have following:

include 'hashtags_table_api.php'; $tag = $_post["tag"]; $res = queryhashtagstable($tag);

in hashtags_table_api.php, there is:

include 'connect.php'; function queryhashtagstable($hashtag) { global $pdo; $sql = "select * `tbl_hashtags` hashtag = :hashtag"; $stmt = $pdo->prepare ( $sql ); echo $hashtag; $stmt->bindvalue(':hashtag', $hashtag); if ($stmt->execute()) { $result = $stmt->fetchall(); echo count($result); } }

connect.php (omitted of import variables):

try { $pdo = new pdo ( "mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password ); $pdo->setattribute(pdo::attr_emulate_prepares, false); $pdo->setattribute(pdo::attr_errmode, pdo::errmode_exception); echo 'connected database'; } catch(pdoexception $e) { echo $e->getmessage(); }

when run code , pass value "hash" initsearchqueryrunnable, result of 0, though have row value "hash" in column hashtag. what's weird when hardcode next in search_query.php:

include 'hashtags_table_api.php'; $tag = 'hash'; $res = queryhashtagstable($tag);

i desired result of 1 query. double checked , $_post["tag"] passing value 'hash' server, reason sql query not recognize beingness equal value in db, though exact same hardcoded value recognized beingness equal.

any clue else need in order have dynamically-passed parameter recognized equal value mysql data?

edit

after testing, realized $_post["tag"] coming through quotes, , strlen[$tag] = 6. think might reason why sql isn't matching query string what's in db. sure plenty strlen['hash'] = 4, sql matches query. how can remove quotation marks value of of $tag, query works? fyi, server running php 5.4.24, if that's relevant.

i solved own problem here. turns out text beingness sent through http post had 2 invisible whitespaces observe tests using strlen , using statement see spaces beingness appended. solution utilize trim() function (http://php.net/manual/en/function.trim.php) , add together $tag = trim($tag) search_query.php before running queryhashtagstable($tag). hope helps else doesn't realize http post request beingness sent invisible characters , needs exact string sql query.

java php android mysql mysqli

No comments:

Post a Comment