-2

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";
?>
7
  • php mysqli extension? Commented Nov 16, 2016 at 4:22
  • 2
    Possible duplicate of php wont connect to mysql Commented Nov 16, 2016 at 4:25
  • I would turn up the logging level and see what its says Commented Nov 16, 2016 at 4:25
  • Add database in the call to mysqli_connect() Commented Nov 16, 2016 at 4:27
  • After doing some more searching I think it's something about the extension. I edited the php.ini file and removed the comment on extension=msql.so. I restarted the httpd service but it still doesn't appear to be loaded when i view the phpinfo() Commented Nov 16, 2016 at 4:37

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

I will try adding the db name
Still have the same issue.
I've taken your code and tested it, I get the same result, just the error 500 page.
Okie, time for error reporting... Add these two lines of code at the very top of the page, just below the opening php tag error_reporting(-1); ini_set("display_errors", 1);
Got it! Thank you
|
1

There are two possible issues with remote mysql connection:

1. Firewall of the server: You must enable incoming connection on port (for. e.g. 3306).

2. User in mysql: You must have a user created (for e.g. jason in this case)

1 Comment

I know it's not a firewall issue and the account is correct. I can connect to it via the command line. I do appreciate the suggestion though.
0

Try connecting this way

$con = mysqli_connect('localhost','root','','db_name');
if($con)
echo "hell yeah its connected";
else
echo "i m boned"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.