0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • You're using PHP 7 or above and use `mysql_query()`. See manual: http://php.net/manual/en/function.mysql-query.php Don't mix `mysqli` and `mysql` calls. – KIKO Software Jul 14 '18 at 08:24
  • Possible duplicate of [Why shouldn't I use mysql\_\* functions in PHP?](https://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Nigel Ren Jul 14 '18 at 08:27
  • Possible duplicate of https://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php – Nigel Ren Jul 14 '18 at 08:27
  • What exactly is the difference? should I downgrade my Php version? – Luis Rivera Jul 14 '18 at 08:28
  • The last duplicate I added is more appropriate, your connecting with `mysqli_connect` and then using `mysql_query`, you should make sure you always use the `mysqli_` functions. – Nigel Ren Jul 14 '18 at 08:30
  • No, please, don't downgrade. The `mysql` extension should not be used. Just use `mysqli` or `PDO` as is suggested everywhere. – KIKO Software Jul 14 '18 at 08:34
  • Ok,thank you for your help – Luis Rivera Jul 14 '18 at 08:35
  • 1
    Possible duplicate of [Can I mix MySQL APIs in PHP?](https://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) – mickmackusa Jul 14 '18 at 10:19

0 Answers0