Domanda

The following code successfully connects me to my mySQL database.

$conn = mysql_connect("localhost", "root", "") or die("Failed to Connect!");
$db = mysql_select_db("MyDB");

I have been experimenting on localhost using XAMPP and phpmyadmin, and everything works correctly. However, today I uploaded my website to my domain (I have bought a domain and web hosting through GoDaddy) and I keep getting "Failed to Connect!" whenever this code runs. The HTML/CSS work correctly, but I cannot connect to mySQL. How can I connect to my database on the server?

È stato utile?

Soluzione

You'll need to change your connection information here:

mysql_connect("localhost", "root", "")

to include your GoDaddy database details instead. Contact GoDaddy for more information on what to use for your account.

Altri suggerimenti

You have not mentioned anything about MySQL database hosting. If you are hosting the database also in godaddy, you should be able to get the connection string and host name from information mentioned here

First, You need to create database in your hosting server (phpmyadmin cpanel) and supply details in your database connect file.

**Looking at your database credentials I can say that you haven't created one yet since
the username is definitely not 'root' and password will be required.**

Sample db_connect file:

    <?php
    $hostname = 'e.g: internal-ra.db-server-123';
    $username = 'e.g: serverone1234d4';
    $password = 'e.g: your_own_password';

    try {
        $pdo = new PDO("mysql:host=$hostname;dbname=database_name_here", $username, $password);
        /*** echo a message saying we have connected ***/
        }
    catch(PDOException $e){
        echo 'Exception -> ';
        var_dump($e->getMessage());
        }
    ?>

And try to use so less as possible the key DIE()

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top