Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid get info from mysql - php
    primarykey
    data
    text
    <p>I am trying to fetch some info from my mysql database, the user unique_id. I have been following tutorial online and changing the parts to just create simple function that returns json object. However when I am trying to launch that method I'm getting errors:</p> <pre><code>No database selected value no of java.lang.String cannot be converted to JSONObject </code></pre> <p>The second one comes because the connection or retrieving data from database did not succeed I guess, but I couldn't find where the problem lies. Here is my code config.php</p> <pre><code>&lt;?php define("DB_HOST", "localhost"); define("DB_USER", "root"); define("DB_PASSWORD", ""); define("DB_DATABASE", "test"); ?&gt; </code></pre> <p>DB_Connect.php</p> <pre><code>&lt;?php class DB_Connect { function __construct() { } function __destruct() { } // Connecting to database public function connect() { require_once 'config.php'; // connecting to mysql $con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); // selecting database mysql_select_db(DB_DATABASE); // return database handler return $con; // Closing database connection public function close() { mysql_close(); } } ?&gt; </code></pre> <p>DB_Functions.php</p> <pre><code>&lt;?php class DB_Functions { private $db; //put your code here // constructor function __construct() { require_once 'DB_Connect.php'; // connecting to database $this-&gt;db = new DB_Connect(); $this-&gt;db-&gt;connect(); if(!$this-&gt;db){ echo "Please try later."; } } // destructor function __destruct() { } // Check if user exists. public function isUserExisted($unique_id) { $result = mysql_query("SELECT unique_id from users WHERE unique_id = '$unique_id'"); if($result === FALSE) { die(mysql_error()); } $no_of_rows = mysql_num_rows($result); if ($no_of_rows &gt; 0) { // user existed return true; } else { // user not existed return false; } } } ?&gt; </code></pre> <p>index.php</p> <pre><code>&lt;?php if (isset($_POST['tag']) &amp;&amp; $_POST['tag'] != '') { // get tag $tag = $_POST['tag']; // include db handler require_once 'include/DB_Functions.php'; $db = new DB_Functions(); $response = array("tag" =&gt; $tag, "success" =&gt; 0, "error" =&gt; 0); if ($tag == 'check') { // Request unique_id $unique_id = $_POST['unique_id']; // check if user is already existed if ($db-&gt;isUserExisted($unique_id)) { // user is already existed - error response $response["error"] = 2; $response["error_msg"] = "User already existed"; echo json_encode($response); } else { $response["error"] = 1; $response["error_msg"] = "USER UNKNOWN!"; echo json_encode($response); } } } ?&gt; </code></pre> <p>And inside my android JSONParses looks like that:</p> <pre><code>public class JSONParser { private static InputStream inputStream; private static JSONObject jSONObject; private static String json; JSONParser(){ inputStream = null; jSONObject = null; json = " "; } public JSONObject getJSONFromUrl(String url, List&lt;NameValuePair&gt; parameters) throws ClientProtocolException{ try { DefaultHttpClient defaultHttpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(parameters)); HttpResponse httpResponse = defaultHttpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); inputStream = httpEntity.getContent(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8); StringBuilder stringBuilder = new StringBuilder(); String line = null; while((line = bufferedReader.readLine()) != null){ stringBuilder.append(line + "n"); } inputStream.close(); json = stringBuilder.toString(); Log.e("JSON", json); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { jSONObject = new JSONObject(json); } catch (JSONException e) { Log.e("ERROR PARSING DATA: ",e.toString()); } return jSONObject; } } </code></pre> <p>And last piece of code, method to work with JSONParser (It is in an Async Call)</p> <pre><code>private JSONObject checkIfUserExists(String unique_id){ List&lt;NameValuePair&gt; parameters = new ArrayList&lt;NameValuePair&gt;(); parameters.add(new BasicNameValuePair("tag", CHECK_TAG)); parameters.add(new BasicNameValuePair("unique_id", unique_id)); JSONObject json=null; try { json = jsonParser.getJSONFromUrl(loginURL, parameters); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } return json; } </code></pre> <p>And I am receiving the error about no Database Selected inside my JSONParser. I have been looking over the code and it seems to be good for me, I know the issue is somewhere small and thats why I cant find it. Any help would be very appreciated. Thanks in advance.</p> <p>@Edit. I have just checked and </p> <pre><code> if($result === FALSE) { echo "DEAD"; die(mysql_error()); } </code></pre> <p>this prints dead, so it seems that my query is wrong, or somewhat not realized but I have no idea what might be wrong with it...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload