-4

Hey guys I have problem with connecting my .php file to database, I've tried different ways but still the same response. Here is my code (updated):

    <?php
$server_name = "localhost";
$mysql_username = "*****";
$mysql_password = "*****";
$db_name = "*****";
$conn = mysqli_connect ($server_name, $mysql_username, $mysql_password, $db_name);
if($conn) {

    echo "Success";

}
else {

    echo "Failed";

}
?>

UPDATE Guys, when I connect to localhost everything works fine but I would like to connect to a server and be able to upload data from different android devices. Currently I use 000webhost.com free service, so might it be the key point and problem that I didn't upgrade that to PRO?

D. B.
  • 488
  • 1
  • 10
  • 20
  • 3
    Seems pretty clear. PHP can't connect to that server. Either it is not there, the traffic is being blocked, or something else. Start by verifying you have a server that is up and can be reached. No one can really help you with this here. –  Sep 24 '17 at 15:01
  • You also need to read this Answer: https://stackoverflow.com/a/549000/1531971 –  Sep 24 '17 at 15:03
  • Do a google search before asking simple questions like this. – Rajendran Nadar Sep 24 '17 at 15:05
  • I did a google search but found nothing. – D. B. Sep 24 '17 at 15:27
  • Are you sure you don't have a typo or mistake in either of those parameters ? You could download a tool such as XAMPP, that comes with some pre-built sample code. You could then compare your connection information with how it has been mentioned in theirs. Unfortunately, no one can help with this, because the parameter values are confidential, so we can't make out whether there is an error in those values or they are the right values at all. – Whirl Mind Sep 24 '17 at 16:21
  • That's the point, I'm still struggling with connecting because it's not working. – D. B. Sep 24 '17 at 16:35
  • @WhirlMind Could it be a problem with my server becuase currently I use 000webhost free services? – D. B. Sep 26 '17 at 19:21
  • It's surely possible. You should write to them explaining your situation, if at all they have support for free services. You should probably change the passwords to some dummy ones in case you have share it with them, Some service providers ask you to share your password for them to check, not sure if it's recommended practice. Also, for testing and development, it's better you use some kind of local installation of PHP, such as XAMPP or WAMPP etc, so that you can have control during development and testing. – Whirl Mind Sep 27 '17 at 08:00
  • @WhirlMind I conntacted my server provider and they responded that using their free service only allows us to connect to localhost and controlling database remotly is possible when we start using thier PRO version. Thank you Whirl Mind for your responses. – D. B. Sep 27 '17 at 10:09
  • If it's okay with you, I shall put my comment as an answer, indicating a possible connection issue with the service provider, you can mark the same as the answer, so it will benefit future readers. Or you could put an answer to your own question and mark it as a community post. – Whirl Mind Sep 27 '17 at 11:28

5 Answers5

3

mysqli_connect requires the arguments to be in the order servername, username, password, databasename. Your order is servername, databasename, username, password.

The code should be:

$conn = mysqli_connect($server_name, $mysql_username, $mysql_password, $db_name);
MinistryOfChaps
  • 1,458
  • 18
  • 31
1

You sequence of parameter is wrong

<?php
$server_name = "*****";
$db_name = "*****";
$mysql_username = "*****";
$mysql_password = "*****";
$conn = mysqli_connect($server_name, $mysql_username,$mysql_password, $db_name);

if($conn) {
    echo "Success";
} else {
    echo "Failed";
} ?>

Correct syntax

mysqli_connect(host,username,password,dbname,port,socket);

Reference mysqi_connect

EDIT

A extra tip

$conn = mysqli_connect("localhost","username","password","db_name");

Put values according to you server.

EDIT 2

MySQL needs to be running on the port 3306 make sure it is correct

Rajendran Nadar
  • 4,962
  • 3
  • 30
  • 51
  • Ok but I don't connect to localhost, so why do /i need port and socket? – D. B. Sep 24 '17 at 15:26
  • As you mentioned these two are optional so that change has nothing to do with it. Still struggling with connecting. – D. B. Sep 24 '17 at 16:33
  • Try the code that I added in my edit. wamp or lamp or mamp or xamp? – Rajendran Nadar Sep 24 '17 at 16:53
  • Is it possible that my server provider 000webhost just doesn't allow to connect to that database because I use their free service? – D. B. Sep 26 '17 at 19:24
  • Nope even I use free service from 000webhost I never had that issue. Wait I will provide the connection code that worked for me. – Rajendran Nadar Sep 27 '17 at 04:26
  • I conntacted my server provider and they responded that using their free service only allows us to connect to localhost and controlling database remotly is possible when we start using thier PRO version. – D. B. Sep 27 '17 at 10:09
  • Ok I was using my webpage for local storage. – Rajendran Nadar Sep 27 '17 at 10:37
0

I think your problem is that you did not instantiate the class, remedied by making it $conn = new mysqli ($server_name, ..... . Also, your sequence of parameters is incorrect. It should be server, user, password and finally database.

In addition, you should be using PHP constants instead of variables for this, so $server_name should be SERVER_NAME. Similarly $mysqli_username, $mysqli_password, and $db_name should be changed. Not that I wouldn't work as you have it, but it just is not proper.

Mike A
  • 19
  • 5
0

From the comments and from the ruling out of other possibilities indicated in the other answers, it is quite possible it is a connection issue with your service provider.

You should write to them explaining your situation, if at all they have support for free services. You should probably change the passwords to some dummy ones in case you have share it with them; Some service providers ask you to share your password for them to check, not sure if it's recommended practice.

Also, for testing and development, it's better you use some kind of local installation of PHP, such as XAMPP or WAMPP etc, so that you can have control during development and testing. If something works on your local installation and not on the remote, having a development installation is useful to isolate and report such issues.

Whirl Mind
  • 884
  • 1
  • 9
  • 18
0

Iy you are using MAMP, the default port for mysql is 8889, but the "traditional" port that php expects to use for mysql is 3306.

So you need to open MAMP and change the MAMP mysql port to 3306, then restart the mysql server. Now the connection should be successful.

Tiago Gouvêa
  • 15,036
  • 4
  • 75
  • 81