PHP MySQL Connectivity

Posted February 7, 2017 9.7k views

Hi there, this is probably a very basic question but I cannot seem to find the answer on google....

I have installed MySQL and Apache on the cloud server and trying to run a PHP program to indicate whether the connection to a database I have created was successful. I have written the PHP (below) on my personal computer and uploaded the file to the web host I use which is godaddy. For some reason every time I reference this PHP file in the URL the page is blank in the web browser (all my other non database PHP files work ok).

Sorry if this is not clear but could someone please help? I am expecting to see the connection to the database in the cloud was successful!

$host = 'xxx'; //188.166.....
$user = 'root';
$pass = 'yyy';
$db = 'zzz';


    echo "Connection Successful";
    echo "Could not connect";



These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
5 answers


I tested the above code locally and on a Droplet and the code works without any issues, though the issue on your end may be with $host. In most cases, you’d need to specify localhost as the host unless the provider specifically requires you to use an IP address.

If you installed Apache, PHP, and MySQL on a Droplet, unless you configured MySQL to bind to an IP specifically, you would need to use localhost as the host.

Thank you for the reply. I tried installing PHP on the Droplet and changing the host to localhost but still no luck (just a blank page).

Maybe I didn’t explain myself properly but is what I am trying to do possible? Ie create a database through MySQL on the Droplet and then upload a PHP file (above) to my web host (godaddy) to pull back information from the MySQL database (which is on the Droplet) and display this information on the web page?


  • @alex878

    Ah, I apologize, I thought you were having issues running this from the Droplet. What we’d be doing here is enabling remote connections. This requires that we modify configuration, but it’s simple and afterwards, we just need to restart MySQL and retest the code.

    Create the Database (on the Droplet)

    From the CLI:

    mysql -u root -p

    Enter the root password for MySQL when prompted and then run the next four commands.

    Change dbtest to your preferred database name.

    Change dbuser to your preferred database username.

    Change 12345678 to a secure password.

    create database dbtest;
    grant all on dbtest.* to 'dbuser'@'%' identified by '12345678';
    flush privileges;

    Note in the above, I’m using % in place of localhost for the user. This is important and required. If you use localhost, the remote connection will fail, even after the next step.

    You can drop your current user by running

    DROP USER 'dbuser'@'localhost';

    Then run the command above to recreate the user.

    Modify MySQL Configuration

    You’ll want to login as root (via SSH) and edit this file:


    So we’d load it up using nano:


    Next you’ll want to find this line and comment it out.

    bind-address            =

    So what you should end up with is:

    #bind-address            =

    Now close out the file and save, then restart MySQL

    systemctl restart mysql

    Now within your code, try to connect once again.

Thank you again for the reply. I have followed the above (creating a new database and commenting out the bind-address line with a #) but unfortunately I am still receiving a blank web page when accessing the the PHP file through the URL (

I thought I would at least get an “Error” result instead of a blank page?

I am not sure if the below information helps? This is where I am at at this stage.

MySQL Workbench
Details for database dbTest
Login Name: alexTest
Authentication Type: Standard
Limit to hosts matching: %
Password: alexTest

PHP file (dbTest.PHP):

$host = 'xxx'; //188.166.....
$user = 'alexTest';
$pass = 'alexTest';
$db = 'dbtest';


    echo "OK";
    echo "Error";

Thank you.


I tried to insert a simple data into MySql through PHP. It was not add a new record and no error. Any idea? please help. Here is the code


// Create connection
$dbh = new mysqli($servername, $username, $password, $dbname) or die (‘I cannot connect to the database because: ’ . mysql_error());

$sql = “INSERT INTO tbl_ghname ( gcode , email) VALUES ('34234342234’,'54535trerterterts’);”;
// use exec() because no results are returned

  • //pattanodom
    //This Work
    //connect from php hostting other Server from Thailand

    // Create connection
    $dbh = new mysqli($servername, $username, $password, $dbname);

    echo “Connection Successful”;
    echo “Could not connect”;

    //But must add user and create Password. in Tap menu “Users & Databases”. and add IP To be able to access database in Tap menu “setting”