Why can’t I connect to my Mysql server?

Asked

Viewed 519 times

0

I am 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 executaeQuery.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 a lot more direct access to the executaeQuery.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
  • 1

    The error shown is not from the Mysql server not connecting, but from the PHP application server that is not responding. How are you running the PHP application? You can access it in the browser using the address http://192.168.0.2?

3 answers

2

By the refused connection error message, it can be deduced that the PHP Web server is not responding at port 80 of address 192.168.0.2. Most likely the IP address or port are not the right ones. IP would not be 192.168.1.2 ?

0

Friend, first of all I strongly recommend not using the mysql_* library, because soon it will be discontinued (deprecated), so there are some much safer alternatives including.

http://www.php.net/manual/en/book.mysqli.php

http://www.php.net/manual/en/class.pdo.php

http://medoo.in/ I use this last one because it already has very strict protections against SQL Injection and a very wide library, however it requires the version of php 5.4+ (because of the change of syntax in relation to the array)

NOTE: The last two runs underneath the mysqli, being this the fastest if your application aims high performance.

Enough of Labla and let’s go to the practical example. mysqli allows writing in two different ways.

I’ve solved a structure just like you’re 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));

0

Try modifying your code and put this, if it doesn’t work select, I switch your id field to an integer so you can remove the simple quotes, and I 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();
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.