Jason Brown Jason Brown - 19 days ago 7
MySQL Question

PHP not connection to MySQL server

I am trying to get my PHP pages to connection to a remote server hosting the MySQL database. I can connect via the command line just fine with the same username and password. Below is a simple test file I created. The only thing I've done to the code is remove the password, but I know that's not the issue. Like I said I can connect via the CLI. This is a Linux install. When viewed from the web browser, all I get is an error 500 page. If I comment out the mysqli statement, the page displays the Connected successfully.

<?php

$servername = "12.0.1.170";
$username = "jason";
$password = "";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection

if (!$conn) {
/ die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

Answer

Per the documentation for mysqli_connect()

$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

mysqli_close($link);

Your code appears to be lacking all four parameters required.

You have:

$conn = mysqli_connect($servername, $username, $password);

Perhaps try:

$conn = mysqli_connect($servername, $username, $password, $database);

with a $database='db_name'; value added into your variables section.

Comments