I am trying to retrieve data from my database in Android, but get no data back. What am I doing wrong and why it is happening? When I go to the link of getData.php I get the following error:
Fatal error: Uncaught Error: Call to undefined function mysql_query() in /storage/ssd1/078/5480078/public_html/denuncias/getdata.php:12 Stack trace: #0 {main} thrown in /storage/ssd1/078/5480078/public_html/denuncias/getdata.php on line 12
Here are my codes:
getData.php
<?php
include 'DatabaseConfig.php';
$con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);
if (!$con)
{
echo('Could not connect: ' . mysql_error());
}
$result = mysql_query("SELECT titulo FROM denuncias");
while($row = mysql_fetch_assoc($result))
{
$output[]=$row;
}
print(json_encode($output));
mysql_close($con);
?>
MainActivity.java
package bughunters.tashfik.demo;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
String sData;
TextView tv;
String Data = "";
String result;
InputStream isr;
Context con;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.test);
new getData().execute("");
}
private class getData extends AsyncTask<String, Void, String> {
String name;
@Override
protected String doInBackground(String... params) {
result = "";
isr = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://luisalonsoriveraibarra.000webhostapp.com/denuncias/getdata.php"); //YOUR PHP SCRIPT ADDRESS
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
isr = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
//convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(isr, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json = jArray.getJSONObject(i);
Data=Data+"\n"+ json.getString("Head");
}
} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error Parsing Data " + e.toString());
}
return "Executed";
}
@Override
protected void onPostExecute(String result) {
tv.setText(""+Data);
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {}
}
}
PS: the tables name is denuncias and the row I want to display is called titulo.