Why can not I connect to my MySQL server?

0

I'm trying to make a simple query using a PHP webservice. This is my current code:

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("id", "1"));
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(
                "http://192.168.0.2/executeQuery.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString());
    }

And this is executeQuery.php

<?php
mysql_connect("localhost:3306","root","password");
mysql_select_db("database");
$q=mysql_query("SELECT * FROM users WHERE id = '".$_REQUEST['id']."'");
while($e=mysql_fetch_assoc($q))
        $output[]=$e;
print(json_encode($output));
mysql_close();
?>

Is there something wrong? Although my server is working fine more direct access to executeQuery.php, I'm getting this exception:

org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.0.2 refused
Error converting result java.lang.NullPointerException
    
asked by anonymous 04.02.2014 / 21:15

3 answers

2

By the connection error message refused, you can deduce that the PHP Web server is not responding on port 80 of address 192.168.0.2. Most likely the IP address or port are not the right ones. Would not IP be 192.168.1.2?

    
04.02.2014 / 22:38
0

My friend, I strongly recommend not to use the mysql_ * library, because soon it will be deprecated, so there are some much more secure alternatives.

link

link

link I use this last one, since it already has very strict protections against SQL Injection and a very ample library, however it requires the php version 5.4+ (because of the syntax change over the array)

NOTE: The last two runs under mysqli, which is faster if your application is high performance.

Enough of so much and go to the practical example. Mysqli allows writing in two different ways.

I resolved the way you are doing:

$link = mysqli_connect("myhost","myuser","mypassw","mybd") or die("Error " . mysqli_error($link));
$query = "SELECT name FROM mytable" or die("Error in the consult.." . mysqli_error($link));
    
04.02.2014 / 22:02
0

Try to modify your code and put this, if it does not work, select that your id field is an integer so you can remove the single quotes, and also added a link to identify the connection.

<?php
$link = mysql_connect("localhost","root","password")or die("Erro ao conectar com o servidor de dados");
mysql_select_db("database",$link)or die("Erro ao selecionar base de dados");
$q=mysql_query("SELECT * FROM users WHERE id = ".$_REQUEST['id'],$link);
while($e=mysql_fetch_assoc($q))
        $output[]=$e;
print(json_encode($output));
mysql_close();
?>
    
05.02.2014 / 13:38