Search code examples
phpmysqlsqlhost

PHP not connecting with server


I have the following PHP code:

(db/connect.php)

enter image description here

(index.php)

enter image description here

And I keep getting the following error:

enter image description here

I have a Linux CPanel and an external webhost: GoDaddy

Why can I not connect?

Thanks :)

Is this the db_user:

enter image description here


Solution

  • Connect to Godaddy
    Your mysqli connection lists "localhost" as the first parameter. You are trying to connect to a database locally. You need to connect to a host remotely, through Godaddy. You will need to lookup the connection information through your CPanel.
    You will need to create and/or locate the following MySql database information:
    1. hostname
    2. username
    3. db_password
    4. db_name

    Using Class mysqli
    You are using: $db = new mysql. You need to use $db = new mysqli_connect
    mysqli_connect needs four paramerters:

    mysqli_connect("host_name","db_user","password","db_name");    
    

    The code in your db/Connect.php should look like this:

    <?php
    $db = mysqli_connect("host_name","db_user","password","db_name");
    // Check connection
    if (mysqli_connect_errno())
          {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
          }
    ?>
    

    http://www.w3schools.com/php/func_mysqli_connect.asp