Jason Brown Jason Brown - 1 year ago 74
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.


$servername = "";
$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 Source

Per the documentation for mysqli_connect()

$link = mysqli_connect("", "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;

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;


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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download