'PHP connection to remote MySQL database acces denied, but it does work in Java

When I try to connect to a remote database using PHP I get an error saying: 'Access denied for user: ...' I am new to PHP and I don't know if my code is correct, here is the code I am using.

<?php
$host = 'adress:port';
$username = 'username';
$password = 'password';
$dbname = 'name'

$con = new mysqli($host, $username, $password, $dbname);

if($con->connect_errno){
    die('Error ' . $this->con->connect_error);
}
echo 'Connected successfully!';
?>

All the variables are correct. When I try the following code in Java I do have acces to said database.

import java.sql.*;
import java.util.Properties;

class MySQLConnect {
    private final String DATABASE_URL = "jdbc:mysql://adress:port/name";
    private final String USERNAME = "username";
    private final String PASSWORD = "password";

    private Connection connection;
    private Properties properties;

    public MySQLConnect() {
    }

    private Properties getProperties() {
        if (properties == null) {
            properties = new Properties();
            properties.setProperty("user", USERNAME);
            properties.setProperty("password", PASSWORD);
        }
        return properties;
    }

    public Connection connect() {
        if (connection == null) {
            try {
                connection = DriverManager.getConnection(DATABASE_URL, getProperties());
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return connection;
    }

}

public class Main {
    public static void main(String[] args) {
        Connection mySQLConnect = new MySQLConnect().connect();
        System.out.println(mySQLConnect);
    }
}

I know I have access to the database and I have permissions to login, but I can't seem to figure out how to connect with it in PHP. I have written the password and username correct, and I have tried alot of combinations for the $host variable but all of them do not work. Am I doing something wrong?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source