Remotely connecting to a MySQL database

Remotely connecting to a MySQL database

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Diagnosing Remote MySQL Connection Failures

You are attempting to access a MySQL database hosted on a third-party service from your local development environment or another server. The immediate blocker is that your browser displays nothing when you open the file directly, but this isn't because of PHP or SQL syntax errors—it's due to how web servers and browsers handle file execution.

When you save a file with a .html extension containing PHP tags (<?php ... ?>), the browser simply renders the raw text. It does not invoke the PHP interpreter. Consequently, the script never runs, $hostname remains undefined in the context of the output, and no database query is executed. To execute PHP code, you must host this file on a server that has the PHP extension installed (e.g., demo.php) and access it via an HTTP URL rather than opening the local file path.

Beyond the file extension issue, there are fundamental architectural constraints regarding remote database connections. Your hosting provider's statement to use ukld.db.5513497.hostedresource.com refers to a hostname that is likely resolvable via DNS or configured in your .htaccess/hosting panel. However, mixing this with an IP address like 113.101.88.97.ukld.db... is incorrect. The string you constructed in the second snippet treats the IP octets as a subdomain prefix of the hostname string, which results in a completely invalid DNS name that will fail to resolve.

Database User Permissions and Host Restrictions

Even if you correct the hostname and ensure the file is served as PHP, the connection will likely fail due to MySQL user privileges. When you created the database user (myusername) via phpMyAdmin or the hosting control panel, you almost certainly set the "Host" field to localhost or %.

  • localhost: Typically restricts connections to TCP/IP port 3306 from the same machine where MySQL is running. If your host uses Unix sockets for local connections, remote IP addresses are explicitly blocked regardless of whether they match the hostname.
  • %: Allows connections from any IP address but may require a specific username that matches the pattern (e.g., myusername@%).

If you attempt to connect remotely using an IP address or a domain name different from the one defined in the user's host privilege, MySQL will reject the connection with an "Access denied for user" error. You must ensure the database user was created specifically for remote access, often requiring a new user entry like myusername@'127.0.0.1' or myusername@'%' depending on how your provider handles external IPs.

Security and Deprecated Extensions

The code snippet provided relies on the mysql_* functions (e.g., mysql_connect, mysql_query). This extension was deprecated in PHP 5.5.0 and removed entirely in PHP 7.0.0 due to critical security vulnerabilities, including SQL injection risks when concatenating variables directly into queries without sanitization.

Your current query:

$query = 'SELECT * FROM ' . $usertable;
$result = mysql_query($query);

is highly dangerous if $usertable contains user input, as it allows attackers to manipulate the structure of the database. Even with hardcoded values, using this extension is not best practice in modern development.

You should migrate to MySQLi or PDO. PDO (PHP Data Objects) provides a unified interface for multiple database drivers and supports prepared statements, which effectively neutralize SQL injection attacks. Here is how you would refactor the connection logic securely:

try {
    $pdo = new PDO(
        "mysql:host=ukld.db.5513497.hostedresource.com;dbname=testdb;charset=utf8mb4",
        'myusername',
        'mypassword',
        [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
    );
    
    $stmt = $pdo->query('SELECT lat FROM test'); // Using prepared statements for dynamic tables if necessary
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        echo 'Name: ' . htmlspecialchars($row['lat']) . '<br>';
    }
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}

Network Connectivity and Firewall Rules

Finally, verify that your hosting provider's firewall allows incoming traffic on port 3306 from your specific IP address. Many shared hosting environments restrict remote MySQL access entirely to prevent unauthorized database usage. If the connection attempt hangs or times out before rejecting it, the issue is likely a network-level block rather than an authentication error.

If you are building a larger application, consider leveraging Laravel's ecosystem if your project requires robust ORM capabilities and built-in migration tools. For instance, Laravel simplifies database connections using environment variables defined in .env files, ensuring credentials never touch your source code repository. While the core concepts of connection strings and permissions remain universal, adopting frameworks like Laravel can significantly reduce the overhead of managing raw connection logic and security patches.

Our network