1

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

gpsrosak
  • 107
  • 1
  • 10
  • 1
    Sorry, but a MySQL server and a HTTP server are two totally different things. You apparently try to mix that... A MySQL Server is not addressed via a URL like you try, but just with a hostname. For that to work the MySQL servers port must be open on the host system. – arkascha Sep 22 '16 at 07:10
  • @arkascha In my cPanel, the database is stored in mySQL (phpMyAdmin), so I just assign my dbHost as localhost? Is that what'd you meant? – gpsrosak Sep 22 '16 at 07:22
  • I have no idea about `cPanel`. But phps `mysql_connect()` function clearly asks for a _host name_: http://php.net/manual/en/function.mysql-connect.php Why don't you read the documentation of the tools you use? Things are clearly explained in there... – arkascha Sep 22 '16 at 07:32
  • And once your are on it you will also see the huge red warning at the top of the page repeating once more that the old mysql extension (`mysql_...()` functions) has been outdated and deprecated long ago. Switch to the newer and safer `mysqli` extension or `PDO`. Do it _now_. – arkascha Sep 22 '16 at 07:33

3 Answers3

2

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.

PravinS
  • 2,640
  • 3
  • 21
  • 25
  • I have change my hostname to localhost as it is on the same server, yet I still cannot connect. It happen to be the hosting problem as I cannot connect even i manually open (phpMyAdmin) in cPanel. – gpsrosak Sep 22 '16 at 07:48
  • what error you are getting now, have you created user "a6410240_cbetTD" and mapped it with database "a6410240_cbetTD"? – PravinS Sep 22 '16 at 07:51
  • I am getting this error now Access denied for user 'a6410240_cbetTD'@'10.1.1.20' (using password: YES) in /home/a6410240/public_html/database.php on line 8, as I get similar error in 000webhost (#1045) – gpsrosak Sep 22 '16 at 09:02
  • i think you have not added user and mappted to the database from cPanel ,also give required privileges to user "a6410240_cbetTD" – PravinS Sep 22 '16 at 09:42
  • I have solved the problem, the user has been created but yesterday the hosting site maybe got a little problem that I cannot access even internally through cPanel. @PravinS – gpsrosak Sep 23 '16 at 03:48
0

The problem you are having was already mentioned in one of the comments, this one to be precise.

For your solution to work, all you need to do is omit the part http:// at the beginning and probably /localhost at the end.

The host is only the domain you are referring to. In this case sql4.000webhost.com. With /localhost you tried to already connect to a database, although your configured database is supposed to be a6410240_cbetTD.

Community
  • 1
  • 1
Björn K
  • 126
  • 1
  • 3
0

MySQL use TCP port 3306 by default ($dbport) and hostname or IP address ($dbhost). For LAMP (Linux-Apache-MySQL-php) you can find a lot of tutorials. Usually MySQL server listens internal port (which can't be reached via Internet) for security purposes. If you familiar with docker, you can simply download examples of LAMP solutions from hub.docker.com.

zirf
  • 334
  • 2
  • 9