phpandroidmysqlweb-hostingmysql-connect

MySQL connect failed. Can't connect to MySQL server on 'http' (4)


I try to connect my android application using JSON Parser to the web hosting. But whenever I try to connect (even just open using url in the browser) I will get the error message.

<?php
  $dbHost = 'http://sql4.000webhost.com/localhost';
  $dbUser = 'a6410240_cbetTD';
  $dbPass = 'xxxxxx';
  $dbName = 'a6410240_cbetTD';

  $conn = mysql_connect ($dbHost, $dbUser, $dbPass) or die ('MySQL connect failed. ' . mysql_error());
  mysql_select_db($dbName,$conn);
?>

This is my database.php file. The full error message is

Warning: mysql_connect() [function.mysql-connect]: Can't connect to MySQL server on 'http' (4) in /home/a6410240/public_html/database.php on line 8.

I have tried change the $conn but still it didn't worked for me.

Thanks


Solution

  • If your database and application is on same server then use "locahost" in $dbhost.

    And if your database and application is on different servers then you need to use IP address or hostname in $dbhost and the database user should be added on database server provided with required privileges.